There is something wrong in my jQuery plugin. I can’t post the whole script because it would be too big, this is a little and modified excerpt. Basically it works this way:
- An ajax call, if result set is empty then backup the element and it’s content (if there is no already backup defined) and the override it’s content
- If result set contains data look for certain elements inside it and use .html() to display the data
But there is somehting wrong in the above. When the call is executed 3 consecutive times children of the backup is sadly empty!
Any help is much appreciated. Here is a simplified version of the control flow:
var backup = function() { this.data('backup', this.clone(true)); }
var onObjectProperty = function(obj) {
// This is where my script fail!!! 3 consecutive times of empty data,
// and children() contains no data!
if($.type(this.data('backup')) !== 'undefined')
console.log(this.data('backup').children());
};
if(!val.error && !val.count) // Not an error, but data is empty
{
// Keyword "this" is the current element in selection loop (on which
// the plugin was invoked)
if($.type(context.data('backup')) === 'undefined')
backup.call(this); // Backup if not already defined
opt.onEmpty.call(context); // Call the function to handle empty data
return true; // Skip the current iteration in the loop
}
// Here we have no errors and result set contains data
onObjectProperty.call(this, obj); // Pass the context and the data
EDIT: found the error, was not cloning the backup before adding it to the DOM!
What’s with the parentheses on the second if-$.type line? =)
Ok got it. Not sure how to read all these “obj”, “context” and “val”, how they fit in, but for what it’s worth I managed to get backups to/fro data going, see below.
Cool idea anyway!