Question

I am writing a plugin using the standard authoring pattern for jQuery, using methods to do different things with, and I'm running into a problem with .each().

I understand from the docs about the difference between $.each() and $(selector).each() yet I can't figure out how to overcome my problem...

In my code I have a method that returns an object which has been stored using $(selector).data() which I can run a $.each() on like so:

$.each($(selector).maps('getdetails'), function(key, value){/* stuff here */});

However, in the scope of jQuery chainability, this isn't how it should be, and I can't understand how to do it like this:

$(selector).maps('getdetails').each(function(key, value){/* stuff here */});

For chainability and ease of use for the user, the latter is preferable. Yet I always get the following error:

TypeError: $(selector).maps("getdetails").each is not a function

Here is the method in question from within the variable object:

getdetails: function(){
    return $(this).data('maps').details;
},

(Yes, before you say it, I know this is incorrect JS, but as I said, it is correctly wrapped in a variable {} in my code.)

Does anyone know how I can manipulate this object into a jQuery object that the latter .each() function will iterate over?

Was it helpful?

Solution

I assume details is an array, which is why details.each is not going to work. You'll need to convert details to a jQuery.init object (the map of jQueried objects), which is done simply by throwing it into the jQuery function:

return $( $(this).data('maps').details );

OTHER TIPS

Are you forgetting the '#' or '.' to signify an id or class, respectively?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top