我需要通过模型实例来表示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);
}
})