0

我有 2 个控制器。第一个,KeywordsIndexController 看起来像这样:

App.KeywordsIndexController = Em.ArrayController.extend({

    contentBinding: "App.Keyword.FIXTURES"

});

...另一个,KeywordsNewController 是一个空白数组控制器:

App.KeywordsNewController = Em.Controller.extend({
});

我也有 2 个视图,KeywordsIndexView 和 KeywordsNewView,每个视图中都有一个函数。KeywordsIndexController 负责列出关键字并删除单个关键字。KeywordsNewController 负责添加一个新关键字,然后路由回关键字列表(索引)。它们看起来像这样:

App.KeywordsIndexView = Em.View.extend({

    templateName: 'templates/keywords/index',

    delKeyword: function(e){
        var obj = App.Keyword.FIXTURES.findProperty("name", e._data.attributes.name);
            App.Keyword.FIXTURES.removeObject(obj);
        }

});

----------------------------------------

App.KeywordsNewView = Em.View.extend({

    templateName: 'templates/keywords/new',

    addKeyword: function(){
        App.Keyword.FIXTURES.pushObject({

            id: App.Keyword.length,
            name: newKey.value,
            scode: '55678',
            campaign: null

        });
        this.get('controller.target.router').transitionTo('keywords.index');
    }

});

问题

这两个事件都按预期自行工作。如果您直接进入新的关键字页面,它会很好地工作。当您在尝试添加新关键字之前访问关键字列表页面时会出现问题。当您通过该路线时,当它尝试添加新关键字时会出现以下错误:

Uncaught TypeError: Cannot call method 'hasOwnProperty' of undefined
Uncaught TypeError: Cannot read property 'name' of undefined

此外,当我尝试打印出 App.Keyword.FIXTURES 数组时,它会作为一个空类返回。

我不确定什么会导致这种行为,非常感谢任何想法/帮助。

额外学分

在测试环境(即FIXTURES)中,除了“App.Keyword.FIXTURES”之外,还有更好的方法来引用该对象吗?

谢谢!

4

1 回答 1

1

在测试环境(即FIXTURES)中,除了“App.Keyword.FIXTURES”之外,还有更好的方法来引用该对象吗?

App.Keyword.all()model您的路线方法中使用:

model: function(controller){
    return App.Keyword.all();
}

这是一个实时数组,如果稍后添加一些模型,它将被更新。

另外,不要将对象推入FIXTURES,只需用于createRecord创建一个新对象:

App.Keyword.createRecord({
    id: App.Keyword.length,
    name: newKey.value,
    scode: '55678',
    campaign: null
});
于 2013-02-15T09:43:28.927 回答