1

我需要通过模型实例来表示 window.location.hash 以通过来自其他模型的事件来控制其状态。用户也可以通过输入地址字符串手动更改哈希值。用户更改必须在应用程序内部进行转换,并且应用程序还可以修改哈希以响应用户与他们所代表的 UI 的交互。有谁知道在这种情况下如何更新窗口哈希?

Hash = Backbone.Model.extend({
    initialize: function(){
        other_source.on('change', this.setHash, this);
        window.on('hashchange', this.update);//user may update hash manually in browser
        //window.on('hashchange', $.proxy(this.update, this)); - I tried this
        //_.bind(this.update, this); - and this
        // it doesn't work
    },
    update: function(data){
        //translate changes inside app
        this.clear();
        this.set(hashToObj(data));
        this.trigger('hash:changed', this.attributes);
    },
    setHash: function(){
        //Apply changes from inside app
        //I need to temporarily remove listening
        //because of inifinitive loop
        window.off('hashchange', this.update);
        window.location.hash = this.attributes;
        window.on('hashchange', this.update);
    }
})
4

0 回答 0