Reputation: 491
I would like to do the following to build up the call to a jquery plugin
jQuery("#wrapper").myPLugin({
direction: direction,
switch myOption
{
case "small":
items:10,
case "big":
items:25,
}
switch otherOption
{
case "small":
prev: {
button: buttonPrev,
key: "left"
},
case "big":
prev: {
button: buttonPrev,
key: "left"
},
}
pagination: ".paginationHolder"
});
Obviously this does not work , but what is the best way to these kind of buildups, is there a general method to dynamically build up a call to jQuery plugins like this
Upvotes: 1
Views: 147
Reputation: 136094
the ternary operator is your friend
condition ? trueResult : falseResult
so in your case that could be
jQuery("#wrapper").myPLugin({
direction: direction,
items: myOption == "small" ? 10 : myOption == "big" ? 25 : 0, // or some default value
prev: otherOption == "samll" ? {button: prevButton, key: 'left'} : otherOption == "big" ? {button: prevButton, key: 'left'} : null,
pagination: ".paginationHolder"
});
Another option is to build up your object outside of the call, after all javascript objects are totally dynamicallly typed:
var setup = new object();
setup.direction = "foo";
switch(myOption){
case "small":
setup.items = 10;
break;
case "big":
setup.items = 25;
break;
}
switch(otherOption){
case "small":
setup.prev = {button: prevButton, key: 'left'};
break;
case "big":
setup.prev = {button: prevButton, key: 'left'};
break;
}
setup.pagination =".paginationHolder"
jQuery("#wrapper").myPLugin(setup);
Upvotes: 1