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 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:
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?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
andLoadResult
are actually called by the plugin how would I retrieve the XML and stick the count into the plugin optioncountMethod
and then the XML itself intoloadRowsMethod
? But the plugin expects functions, not plain values. Is there a best way to do this sort of thing?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.