Ember事件处理程序中缺少view.conf或templateContext

Missing view.context or templateContext in an Ember event handler

本文关键字:view conf templateContext 事件处理 程序 Ember      更新时间:2024-05-27

我试图将填充视图的对象推送到数组中,但引用不知何故丢失了。我有一个Ember视图,带有一个定义的eventManager:

FrontLine.NewProductButton = Em.View.extend({
    tagName: 'button',
    classNames: ['addtl_product',],
    templateName: 'product-button',
    eventManager: Ember.Object.create({
        click: function(event, view) {
            FrontLine.ProductsController.toggleProductToCustomer(event, view);
        }
    })
})

该视图使用#each辅助对象渲染一组按钮,这些按钮使用来自ProductsController中对象的属性进行渲染。那部分效果很好。当我点击其中任何一个按钮时,点击事件就会启动,并执行我要求的任何操作,包括成功调用我从ProductsController:中指定的处理程序函数(toggleProductToCustomer

FrontLine.ProductsController = Em.ArrayController.create({
    content: [],
    newProduct: function(productLiteral) {
        this.pushObject(productLiteral);
    },
    toggleProductToCustomer: function(event, view){
        FrontLine.CustomersController.currentCustomer.productSetAdditional.pushObject(view.context);    
    }
});

我正试图使用该函数将其属性填充该视图的对象推送到数组中。我的应用程序中的另一个地方(一个简单的搜索字段),使用pushObject(view.context),效果非常好。然而,在这里,所有被推入数组的都是undefined。我试着使用view.templateContext,但效果并不好。当我尝试console.log从这些函数中插入按钮的view对象时,我得到了我所期望的:

<(subclass of FrontLine.NewProductButton):ember623>

但无论是view.context还是view.templateContext都返回undefined。如何访问我要访问的对象,以便将其添加到我的数组中?

简单的答案是一个字母的差异:

view.content

或:

view.get('content')

在该特定情况下提供源对象,而不是view.context

(到目前为止,我对Ember唯一真正的挑战是,对象和属性的访问器因情况而异,而且没有真正的文档。有时对象在view.conf,有时在view.content,有时_parentView.content等。如果有一张图表,里面有无数不同的语法来访问相同的数据,那就太棒了,这取决于你要通过哪个特定的光圈来获取数据。我还在发现它们…)