Backbone.js state management / view initialization based on url fragment

前端 未结 2 466
遇见更好的自我
遇见更好的自我 2021-01-31 04:24

I\'m trying to keep track of the state in this app using Backbone.js:

\"enter

I ha

2条回答
  •  清歌不尽
    2021-01-31 04:54

    2) How can I avoid my views setting an attribute on the model which they themselves listen for?

    You don't. Your model should be doing validation on any attributes you try to update so your view needs to listen in case your setting of the attribute failed or the validation changed the value.

    What your view does is, it's tries to set a value on the model, the model then either sets it, changes the data and sets it, or rejects it. Your view needs to be updated accordingly.

    3) How can I push a new state based on a part of the model?

    // for each attribute
    _.each(["attribute1", "attribute2", "attribute3", ...], _.bind(function(val) {
        // bind the change
        // bind the setState with `this` and the `value` as the first parameter
        this.bind("change:" + value, _.bind(this.setState, this, value));
    }, this));
    

提交回复
热议问题