[英]Properly Attaching a Backbone.Marionette LayoutView to the Application
[英]Passing a model to a LayoutView Backbone.Marionette
我试图将模型传递给LayoutView,以便可以在视图中编辑特定的模型属性。
在我的ItemView中 ,有一个事件可以抓取所选模型并将其分配给我的全局App-
var ItemView = Backbone.Marionette.ItemView.extend({ template: _.template(ItemTemplate), tagName: "tr", attributes: function(){ return { "id": this.model.get('id'), "class": "tr" }; }, events: { "click #edit" : "edit" }, edit: function() { App.EditModel = this.model; console.log(App.EditModel); // <-- prints out the model successfully App.trigger('edit'); } })
然后,我在编辑视图中使用App.EditModel传递给模板-
var Layout = Backbone.Marionette.LayoutView.extend({ model: App.EditModel, template : _.template(EditTemplate), regions : { "HomeRegion": "#home" } }); return Layout; });
在浏览器中,我得到了“未捕获的ReferenceError:未定义firstName”,因为未正确映射模型。
我该如何处理?
第一次看到布局代码时,扩展功能就会运行,并接受所有选项,这时我App.EditModel
不存在,因此模型存储为undefined
如果要将模型传递给LayoutView
,则应在实例化它时执行此操作,即new Layout({model: this.model});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.