Search code examples
javascriptbackbone.jsbackbone-stickit

Set global setOptions for Backbone.Stickit.js bindings


Is there any way of setting global setOptions for Backbone.Stickit.js?

That way I can avoid having to set validate: true as well as other custom options on every binding:

bindings = { '.someEl': { observe: 'prop1' , setOptions: { validate: true } } , '.someOtherEl': { observe: 'prop2' , setOptions: { validate: true } } , '.yetAnotherEl': { observe: 'prop3' , setOptions: { validate: true } } };

I had seen some post about using the * selector with .addHandler:

Backbone.Stickit.addHandler({
     selector: '*',
     setOptions: {validate: true}
});

But that didn't work for me.

I'm sure there's a simple way that I'm missing but for now my hack was to create a method that parses my property name:

function stickTo(propName, options) {
    _.extend({observe: propName}, {setOptions: {validate: true}}, options);
}

...

bindings: {
    '.someEl': stickTo('prop1')
}

stickTo sets all my default options and takes an optional parameter that overrides my defaults...


Solution

  • The handler should have worked. I setup a fiddle which logs the arguments of Model.set to the console, every time input changes:

    http://jsfiddle.net/px6UP/39/

    Backbone.Stickit.addHandler({
        selector: '*',
        setOptions: {validate:true}
    });