VΛVΛV
VΛVΛV

Reputation: 93

Jquery Plugin Colaboration

I have some code but i think its not that essential, this is more of a theoretic question.

Lets say i have a plugin that generates a series of dates in a box.

I am extending this plugin so i can create it in multiple instances What would be the proper format for the function so i can use it on multiple occasions.

Then i use a scroller plugin to scroll in that box with the mouse in case the dates exceed the size of the box.

Both plugin code is mine so i can edit any part with no problem.

Where i get stuck is to how can i pass options that will change the result of the plugins if they are working together. Lets say i generate the dates and i want to scroll them vertically instead of horizontally.

I am looking for some function initiation pattern that i would be able to pass the corresponding secondary function for motion - lets say the scroller function.

Upvotes: 0

Views: 37

Answers (1)

Josh Allen
Josh Allen

Reputation: 122

One approach would be to add an options parameter.

function plugin(opts) { 
  opts = opts || {};
  var defaults = {}; //some default options

  //whatever options that were passed in win
  for(key in opts) defaults[key] = opts[key];

  //rest of plugin code
}

Somewhere else where you want to use the new options

plugin({
  scroller:true
});

This approach has the advantage that you don't have to pass in the options parameter at all. So your old code will still work if you don't modify the plug in too much.

Upvotes: 1

Related Questions