开发者

jQuery - Getting XML data passed as an option to a plugin

开发者 https://www.devze.com 2022-12-13 14:25 出处:网络
This is someone elses code that I am trying to understand and modify... The basics are that I am trying to get some XML and load it into a custom开发者_StackOverflow社区 plugin.

This is someone elses code that I am trying to understand and modify...

The basics are that I am trying to get some XML and load it into a custom开发者_StackOverflow社区 plugin.

I have something like the following (the PHP page being hit takes the dothis property and switches based on it, returning either an integer or the actual XML):

(function() {

var CountResult = function() {

    var count = 0;
    $.ajax({ 
        async: false,
        url: 'getstuff.php', 
        data: {dothis: 'countit'}, 
        type: 'POST',
        success: function(data) {
            count = $(data).find('returned').text();
            count = $.trim(count);
        }
    });

    return count;
}; 


var LoadResult = function(stuff1, stuff2) {

    $.post('getstuff.php',
        {stuff1: 'one', stuff2: 'two', dothis: 'getit'},

        function(data) {

            //do stuff with returned data

        }

    ); 

};


$(function() {

    $('#SomeDiv' ).myPlugin({
        countMethod: CountResult,
        loadRowsMethod: LoadResult
    });

    });

})();

Some questions I have about this:

  1. Why is the anonymous function at the top lacking the jquery $? I would have expected to see (function($){} rather than (function(){}. How does that work with the regular jquery call at the bottom $(function() {}? Can anyone explain?

  2. The methods that are being passed to the plugin hit a PHP page which returns the desired data about an XML document (the PHP either counts the nodes in the document, returning it in a node called "returned" or returns the nodes themselves, wrapped in the "returned" node). Wouldn't it be more efficient to just grab the XML directly with jQuery, bypassing the PHP page? How would I do this? Since CountResult and LoadResult are actually called by the plugin how would I retrieve the XML and stick the count into the plugin option countMethod and then the XML itself into loadRowsMethod? But the plugin expects functions, not plain values. Is there a best way to do this sort of thing?

  3. What is the purpose of creating the functions like:

    var Something = function(){}

    rather than:

    function Something(){}

EDIT: The main issue I'm having is wanting to load the XML directly in jQuery rather than go through PHP. I can't see how to do it, getting the results and passing the pertinent data into the two private functions, within the framework I've got here. Maybe it isn't meant to be done this way? Not sure.


The main issue I'm having is wanting to load the XML directly in jQuery rather than go through PHP. I can't see how to do it, getting the results and passing the pertinent data into the two private functions, within the framework I've got here. Maybe it isn't meant to be done this way? Not sure.

They arent coded to do that... they are designed to get the data themselves... so in short no - you would need to make your own functions to pass in that do the same things as the ones hard coded in the plugin (excluding the changes you see fit to make - like using the xml file instead of php, or loading the xml file only once instead of on each call to the function).


$(function() {}); is shorthand for $(document).ready(function(){});

You only need to use (function($){})(jQuery) to be compatible with jQuery.noConflict() and to namespace $ = jQuery within the plugin code. If you arent worried about that and are using $ globally it makes no difference if you pass the '$' in or not. Granted being compat with noConflict is gernally a compulsory thing but it will work either way - well until you need noConflict anyhow.

As far as the plugin expecting functions... jsut pass in annonymous functions like:

$('#SomeDiv' ).myPlugin({
        countMethod: function(){/*body*/},
        loadRowsMethod: function(){/*body*/}
    });

As far as the way you defin functions.. when you do var myfunc = function(){}; that means the function only exists in the scope in which it was created (for example within the execution of the function it was defined in) - whereas i believe defining a function like function myfunc() makes that function global (attached to the window object).

0

精彩评论

暂无评论...
验证码 换一张
取 消