Backbone.js - 将 2 个模型传递给 1 个视图

发布于 2024-12-09 12:03:07 字数 541 浏览 1 评论 0原文

我试图将 2 个模型传递给视图,但似乎不起作用。这是我的示例: http://jsfiddle.net/kahhor/jp4B6/14/ 作为您可以看到第二个警报显示未定义...

可能是我的方法错误。我想做的是:在 View1 中将事件 'change' 绑定到 Model1...而不是单击 中的按钮View2,调用Model1中更改值的函数,并自动渲染View1,因为它绑定到了更改事件。

但不要忘记 View2 也有自己的 Model2,它是我在视图外部创建的,然后像 new View2({model:Model2}) 一样传递它;

一开始可能看起来很混乱,但我认为主干可以做的事情很简单。我只是不知道该怎么做:)

谢谢,

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...

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.

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 :)

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随心而道 2024-12-16 12:03:07

您可以像这样访问自定义参数(选项)

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 中找到

you can access custom parameters (options) from

window.PopupView = new PopupView({ model: LeftNotificationM, model2: PopupM});

like this:

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
});

Conclusion: "unrecognized options" of a view can be found in this.options

执笏见 2024-12-16 12:03:07

我刚刚通过 Google 找到了这个问题和 Sled 的答案,它对我帮助很大 - 但只是为了更新这个 2 年前的问题,也许可以让其他一些 Google 员工免于头痛:

主干视图不再自动附加传递给
构造函数作为 this.options,但如果您愿意,您可以自己完成。

Vitaliy 对类似问题的回答向您展示了如何:

initialize : function (options) {
  this.options = options || {};
}

I just found this question and Sled's answer via Google and it helped me a lot - but just to update this 2 year old question and perhaps save some other Googlers the headache:

Backbone Views no longer automatically attach options passed to the
constructor as this.options, but you can do it yourself if you prefer.

Vitaliy's Answer on a similar question shows you how:

initialize : function (options) {
  this.options = options || {};
}
狼性发作 2024-12-16 12:03:07

添加此补丁并直接传递任何选项以查看。

    Backbone.View.prototype._configureWithoutThis = Backbone.View.prototype._configure;
    Backbone.View.prototype._configure = function(options) {
      this._configureWithoutThis(options);
      _.extend(this, this.options);
    }

Add this patch and pass any options directly to view.

    Backbone.View.prototype._configureWithoutThis = Backbone.View.prototype._configure;
    Backbone.View.prototype._configure = function(options) {
      this._configureWithoutThis(options);
      _.extend(this, this.options);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文