BrownChiLD
BrownChiLD

Reputation: 3713

How to properly set a plugin's options using an object

I'm almost ashamed to ask.. but can't seem to figure this one out..

Normally it's done like so...

instance_name = $("#select-field").magicSuggest({
        valueField: 'id',
        displayField: 'title',            
        maxSelection: 1
    });

I wanted to make it like so.. so i can make a function out of it...

options ={ 
           valueField: 'id',
           displayField: 'title',            
           maxSelection: 1
         };

instance_name = $("#select-field").magicSuggest(options);

I tried this and there are no errors, but it doesnt work..

fiddled: http://jsfiddle.net/B6Gtm/6/

Upvotes: 1

Views: 39

Answers (1)

Jamiec
Jamiec

Reputation: 136104

You've put your initialisation in a function where you have redefined the variable options so it is scoped to that function, but you've not passed in your options

options = {    
    data: ['Paris', 'New York', 'Japan'],
    valueField: 'id',
    displayField: 'name',
    hideTrigger: true,
    maxSelection: 1
};
function doMe(options){

    var instance_name2 = $("#select-field2").magicSuggest(options);
}
doMe();

So your two examples are by no means the same, if they were then it would work fine. Demonstrated here by making the 2 the same: http://jsfiddle.net/B6Gtm/7/

So this works:

options = {    
    data: ['Paris', 'New York', 'Japan'],
    valueField: 'id',
    displayField: 'name',
    hideTrigger: true,
    maxSelection: 1
};
var instance_name2 = $("#select-field2").magicSuggest(options);

as does

options = {    
    data: ['Paris', 'New York', 'Japan'],
    valueField: 'id',
    displayField: 'name',
    hideTrigger: true,
    maxSelection: 1
};
function doMe(options){

    var instance_name2 = $("#select-field2").magicSuggest(options);
}
doMe(options);

Upvotes: 1

Related Questions