views:

71

answers:

2

I know how to do plugins, but how do I do nested options like:

var defaults = {
    spacing:10,
    shorten_to:50,
    from_start:0,
    from_end:2,
    classes: {
        test:'testing'
    }
};

i know that isn't right, i just dont know how to write the proper syntax when I want to do something like this:

$('#breadcrumbs').breadcrumbs({classes{test:'new_example'},spacing:12})

other suggestions are welcome, im in need of the ability to custom class names and there are 7, so rather than making something like test_class, example_class, etc id like it cleaner and neater like the example above.

+3  A: 

Actually that is correct. Your notation there is known as JSON, and it's an extremely simple notation (see json.org)

var someobject = { prop: 'prop' };
var anotherobject = { name: 'name' };
someobject.someproperty = anotherobject;

Is equivalent to

var someobject = { prop: 'prop', { name: 'name' }};

In your second example, you're just missing a colon.

$('#breadcrumbs').breadcrumbs({classes:{test:'new_example'},spacing:12})
Joel Potter
thanks, very close, but now, if i dont set ALL of them they are all, but the defined one, undefined. E.g. $('#top-breadcrumbs').breadcrumbs({classes:{breadcrumb_item:'the_bc'},delay_time:100}); makes EVERY one undefined, BUT breadcrumb_item. I want it to work like the normal jQuery default or custom way
Oscar Godson
@Oscar, for that you'll need Jquery's extend function with deep copying as shown by James.
Joel Potter
+2  A: 

Your plugin takes one options parameter and people pass parameters into the plugin using an object literal. You then use $.extend to combine the options with the defaults. Here is a pattern for a plug-in you can copy.

//Create closure
(function($) {

    var defaults = { //Default settings for breadcrumbs
        async: false,
        race: 100,
        interval: 1,
        classes: {
            test:'testing'
        }
    };

    //Plugin definition
    $.extend({

        //Execute the functions added to the stack
        breadcrumbs: function(options) {

            options = $.extend(true, defaults, options);

            //Loop through each item in the matched set and apply event handlers
            return this.each(function(i) {

                //Code here , this = current selection
            });
        }
    });

// end of closure and execute
})(jQuery);

You would call this plug-in like so

$('div').breadcrumbs({async:true, interval:2, classes: {another: true}});
James Westgate
i have all of that, i was curious how to nest the defaults :)
Oscar Godson
Ok. Updated. $.extend should do a 'deep extend'.
James Westgate
awesome, thanks... but the only issue is after i added the {} in the extend, it doesnt change the defaults?
Oscar Godson
$('#top-breadcrumbs').breadcrumbs({classes:{breadcrumb_item:'the_bc'},delay_time:100}); for example, the breadcrumb_item still == 'breadcrumb-item'
Oscar Godson
@Oscar, @James, that should be `$.extend(true, defaults, options);`. deep is a boolean per http://api.jquery.com/jQuery.extend/.
Joel Potter
Thanks Joel, updated.
James Westgate
THANK YOU! you both rock!
Oscar Godson