문제


I have the following View:

var PageView = Backbone.View.extend({
    model: Models.MyModel,

    initialize: function () {
        this.state = window.state;
        this.state.on("change", this.render, this);
    },
    render: function () {
    }
});

The state is an another Model that will contain different global settings like: pageNumber, pageSize and etc.

So my question is: is it possible to change this.state.on("change", this.render, this); to something like:

this.state.on("change:pageNumber=2", this.render, this);

i.e. this.render will be executed after state is changed and only if pageNumber property will be equal to 2.
I know that I can just place if statement into render method but if there is way to do that like above it will be greater.

Thanks.

도움이 되었습니까?

해결책

Backbone does not offer a filtering mechanism on events, but you could alter your state model to trigger custom events with the signature you wish.

For example, let's say state is an instance of this class

var EqualBindModel = Backbone.Model.extend({

    arm: function(attribute, watchvalue) {
        this.on('change:'+attribute, function(model, val, options) {
            if (watchvalue === val)
                model.trigger('change:'+attribute+'='+val, model, val, options);
        });
    }

});

you could then setup your custom event with

var state = new EqualBindModel();
state.arm('pageNumber', 2);

and listen to with

state.on("change:pageNumber=2", function(model, value, options) {
    console.log('event change:pageNumber=2');
});

And a demo http://jsfiddle.net/ZCab8/1/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top