Backbone.js 为主干网中的currentView对象返回了未定义的
我正在测试布局如何侦听其子视图的自定义事件 我创建了一个JSFIDLE,其中有一个布局,两个区域,我实例化了两个ItemView,并在布局的区域中显示它们。小提琴是用咖啡做的Backbone.js 为主干网中的currentView对象返回了未定义的,backbone.js,coffeescript,marionette,Backbone.js,Coffeescript,Marionette,我正在测试布局如何侦听其子视图的自定义事件 我创建了一个JSFIDLE,其中有一个布局,两个区域,我实例化了两个ItemView,并在布局的区域中显示它们。小提琴是用咖啡做的 <div id="region"></div> <script id="layout-tmpl" type="text/_"> <h3>Heading</h3> <div id="content"></div> &l
<div id="region"></div>
<script id="layout-tmpl" type="text/_">
<h3>Heading</h3>
<div id="content"></div>
<div id="content2"></div>
</script>
<script id="item-tmpl" type="text/_">
<form>
<label>Firstname:</lable>
<input type="text" name="firstname" value="<%= firstname %>" />
<input type="button" value="Save" id="save" />
</form>
</script>
我希望布局能够侦听ItemView的自定义事件。在onShow中,我在区域管理器中循环并尝试访问currentView对象,但它返回未定义的
如果我在SimpleLayout类之外附加相同的事件处理程序,并且在显示ItemView之后,布局处理程序会正确地处理自定义事件
谢谢你的帮助 我认为现在的情况是,当调用布局的
onShow
时,视图实际上还没有准备好:
layout = new SimpleLayout()
region.show layout
//此处调用onShow,没有要绑定的视图,因为它们尚未创建
layout.main.show new SimpleItemView model: (new Backbone.Model firstname: "Olivier")
layout.other.show new SimpleItemView model: (new Backbone.Model firstname: "Travis")
// now the views are ready, they can be bound to
_.each layout.regionManagers, (region, name) =>
console.log region.currentView
region.currentView.bindTo region.currentView, "custom:event", () ->
alert "HELL YEAH"
这显示了
custom:event
被触发。我想发生的是,当布局的onShow
被调用时,视图实际上还没有准备好:
layout = new SimpleLayout()
region.show layout
//此处调用onShow,没有要绑定的视图,因为它们尚未创建
layout.main.show new SimpleItemView model: (new Backbone.Model firstname: "Olivier")
layout.other.show new SimpleItemView model: (new Backbone.Model firstname: "Travis")
// now the views are ready, they can be bound to
_.each layout.regionManagers, (region, name) =>
console.log region.currentView
region.currentView.bindTo region.currentView, "custom:event", () ->
alert "HELL YEAH"
这显示了
custom:event
被触发。是的,我让它以完全相同的方式工作,在我的类定义中,当在布局上调用onShow时,区域中没有视图。这对我来说现在很明显(以前不是:))。但是,我希望在Layout类中有一个可以绑定到视图的位置,而不是在添加子视图后调用这段代码。明白我的意思吗?是的,我让它以完全相同的方式工作,在我的类定义中,当在布局上调用onShow时,区域中没有视图。这对我来说现在很明显(以前不是:))。但是,我希望在Layout类中有一个可以绑定到视图的位置,而不是在添加子视图后调用这段代码。明白我的意思吗?