Ya Zhuang
Ya Zhuang

Reputation: 4660

jQuery plugin default options prefix, string combine

I'm wring a jQuery plugin, and I want the divs created inside of the plugin can be customized the ids, which i mean:

this is what i've done:

(function($) {
    $.fn.plugin = function (options) {
        var defaults = {
            box_id = "nos-box_id",
           shoes_id = "nos-shoes_id"
    }
    ...
    ...
    ...
   return this;

})(jQuery);

and i want this:

(function($) {
    $.fn.plugin = function (options) {
        var defaults = {
            plugin_prefix = "nos-",
            box_id = _somethinghere_ + "box_id",
           shoes_id = _somethinghere_ + "shoes_id"
    }
    ...
    ...
    ...
   return this;

})(jQuery);

to make "somethinghere" equals to defaults.plugin_prefix(in this case, "nos").

can anybody help me figure this out?

Thx !

Upvotes: 1

Views: 301

Answers (1)

mu is too short
mu is too short

Reputation: 434975

Don't build the id attributes until you have the prefix in hand. Leave plugin_prefix in your defaults but move box_id and shoes_id to somewhere else.

$.fn.plugin = function (options) {
    var defaults = {
        plugin_prefix: "nos-",
        // other options ...
    };
    options = $.extend({ }, options, defaults);

    var ids = {
        box_id:      options.plugin_prefix + "box_id",
        shoes_id:    options.plugin_prefix + "shoes_id",
        pancakes_id: options.plugin_prefix + "pancakes_id",
        // other ids you may need...
    };
    //...

Upvotes: 1

Related Questions