监听 Marionette 对象中变量的变化?

Listen change in a variable in Marionette object?

哪个事件方法可以帮助捕获对 marionette 对象中定义的 'variable' 值集的更改。

例如:

var LoginModule = Marionette.Object.extend({

    initialize: function(options) {
        this.state = 'pending';
    },

    init: function(model) {
        model.fetch({validate:true})
        .done(function() {
            this.state = 'success';
        })
        .fail(function(res){
            console.log(res);
            this.state = 'failed';
        });

        return model;
    }
});

这里我想检测变量 this.state 的变化。是否有触发一次的选项,例如 listeToOnce?

用法:

var loginM = new LoginModule(); loginM.init(model);

编辑: 我想我为您找到了更好的解决方案。像下面这样尝试。 首先,像这样更改您的 init 方法。

init: function(model) {
        model.fetch({validate:true})
        .done(function() {
            this.state = 'success';
            this.triggerMethod('state:changed');
        })
        .fail(function(res){
            console.log(res);
            this.state = 'failed';
            this.triggerMethod('state:changed');
        });

        return model;
    }

之后你可以像下面这样捕捉事件 state:changed

this.on("state:changed", function(){
     //handle your changed state
});