如何触发从子视图到父视图中backbonejs和requirejs事件 [英] How to trigger an event from child view to parent view in backbonejs and requirejs

查看:398
本文介绍了如何触发从子视图到父视图中backbonejs和requirejs事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态创建一个子视图parentview,我已经引发了子视图到父视图的事件,我传递子视图模型与某些属性设置父视图。如今在父视图我必须采取的模式,并加入到收藏。这里是我的code
子视图触发code

I have a parentview which dynamically creates a child view and I have triggered an event from child view to parent view and I am passing the model of the child view to parent view with some properties set. now in the parent view i have to take the model and add to collection. here is my code child view trigger code

$(this.el).trigger('added', this.model);

parentview事件

parentview event

events: {
            "added": "addNewAttribute"
        },
    addNewAttribute: function (model) {
        console.log(model);
        this.collection.add(model);
        console.log(this.collection);
    }

的console.log返回undefined。有人可以让我知道如何在模型传回父视图?

console.log returns undefined. can someone let me know how to pass the model back to parent view?

感谢

推荐答案

我可能是关闭基地,但我猜想,孩子认为没做包含此code函数绑定:

I may be off base but I'd guess that the child view did not do binding for the function that contains this code:

$(this.el).trigger('added', this.model);

因此​​当你进入那个code为,功能本并不指向子视图,它指出了一些DOM对象或东西。然后this.model不指向你所期望的。

As a result when you get into the function that code is in, "this" doesn't point to the child view, it points to some DOM object or something. Then this.model doesn't point to what you expect.

例如:

initialize : function () {
  _.bindAll(this, "addFunction");
}

addFunction : function () {
  $(this.el).trigger('added', this.model);
}

这篇关于如何触发从子视图到父视图中backbonejs和requirejs事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆