Backbone.js - 将 2 个模型传递给 1 个视图 [英] Backbone.js - passing 2 models to 1 view
问题描述
我正在尝试将 2 个模型传递给视图,但它似乎不起作用.这是我的例子:http://jsfiddle.net/kahhor/jp4B6/14/ As您可以看到第二个警报显示未定义...
I'm trying to pass 2 models to the view, but it seems it is not working. Here is my example: http://jsfiddle.net/kahhor/jp4B6/14/ As you can see second alert is showing undefined...
可能是我的方法不对.我想要做的是:在 View1
绑定事件 'change'
到 Model1
... 比点击 中的按钮View2
,调用Model1
中改变值的函数,自动渲染View1
,因为它绑定了change事件.
May be I have wrong approach. What I'm trying to do is: in View1
bind event 'change'
to Model1
... Than by clicking button in View2
, call function in Model1
which changes value, and automatically render View1
, since it was binded to change event.
但是不要忘记 View2
也有它自己的 Model2
,它是我在视图之外创建的,然后像 new View2({model:模型2});
.
But don't forget that View2
has also its own Model2
, which I created outside the view and than passed it like new View2({model:Model2});
.
一开始可能看起来很混乱,但我认为主干可以做的很简单.我只是不知道该怎么做:)
It might looked confusing at first, but I think it is simple thing that backbone can do. I just don't know how to do it :)
谢谢,
推荐答案
您可以从
window.PopupView = new PopupView({ model: LeftNotificationM, model2: PopupM});
像这样:
window.PopupView = Backbone.View.extend({
// code left out
initialize: function () {
this.model.bind('change:notification_num', this.render);
alert(this.model);
// your model2 option:
alert(this.options.model2);
},
// code left out
});
结论:可以在this.options
这篇关于Backbone.js - 将 2 个模型传递给 1 个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!