mxriverlynn / backbone.picky

selectable entities as mixins for Backbone.Model and Backbone.Collection
129 stars 29 forks source link

Custom options, passed to event handlers #26

Open hashchange opened 10 years ago

hashchange commented 10 years ago

With custom options, it is possible to send additional information to event handlers.

It works by passing an arbitrary, custom option (or a whole bunch of them) to any method. The option doesn't affect the operation of Backbone.Picky, but it is passed on to the event handlers as the last argument.

myCol = new SingleCollection([myModel]);
myCol.on("select:one", function (model, options){ 
  if (options) console.log("Selected while foo=" + options.foo);
});

myCol.select(myModel, {foo: "bar"});    // prints "Selected while foo=bar"

Options get passed around to all event handlers which are running. In the example above, the event handler is set up for the collection. But it will also pick up an option passed to the select method of the model, for instance.

myModel.select({foo: "baz"});    // prints "Selected while foo=baz"

The feature is based on my previous commits. Tests and documentation are included.