更新值传递到骨干视图 [英] Update values passed to Backbone view
问题描述
在addone I M打造的观点一样,新的对象
In addone i m creating new object of view like that
this.section = "aaa";
var sectionview = new AA(model:this.model,section:this.section);
部分是I M传递到AA view.But传递部分的价值得到在结束更改添加一个像这样接连鉴于全局变量
section is global variable of another view that i m passing to AA view.But after passing section its value get change at end of add one like this
this.section = "aaa";
var sectionview = new AA(model:this.model,section:this.section);
.
.
.
.
.
.
this.section = "sss";
然后我怎么可以更新部分,我在创建视图AA的时间过去了价值?
预期的答案是
then how i can update value of section that i passed at time of creation of view AA??? Expected answer is
this.options.section = "sss" not "aaa"
在AA视图
推荐答案
通常的方法来这样的事情是延长 Backbone.Events
建立一个全球性的pub / sub事件调度:
The usual approach to this sort of thing is to extend Backbone.Events
to build a global pub/sub event dispatcher:
window.pub_sub = _({}).extend(Backbone.Events);
然后您的视图可以侦听来自事件的 pub_sub
:
initialize: function() {
this.listenTo(pub_sub, 'change:section', this.section_changed);
//...
},
section_changed: function(section) {
this.section = section;
// And whatever else needs to happen...
}
然后,当你更改的部分触发事件:
Then trigger the event when you change the section:
pub_sub.trigger('change:section', new_section_value);
您会希望通过一个函数调用的地方漏斗所有更改全局部分,以确保事件被触发,但你应该这样做无论如何之类的事情。
You'd want to funnel all changes to the global section through a single function call somewhere to ensure that the events are triggered but you should be doing that sort of thing anyway.
演示: http://jsfiddle.net/ambiguous/rPtfS/
如果您需要这些设置,坚持再改 pub_sub
来一个全局设置模型,并用通常的模型持久性机制。
If you need these settings to persist then change pub_sub
to a global settings model and use the usual model persistence mechanisms.
这篇关于更新值传递到骨干视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!