Ember阵列控制器 - 无法加载功能

时间:2013-05-14 13:39:03

标签: ember.js

我是ember的新手,我正在尝试将一个小数组加载到控制器中。问题是,我在cardsController中定义的addCard函数没有出现,我收到错误:“object function()没有方法'addCard'”。我究竟做错了什么?我使用以下内容:

车把-1.0.0-rc.3.js,
烬-1.0.0-rc.3.js,
烬-data.js

这是我的代码:

App = Ember.Application.create({
    ready: function(){
        //Populate content[] in cardController
        App.GetCards();
    }
});

App.GetCards = function(){
    card1 = App.Card.create({
                id: 0,
                title: 'Alabama',
                desc: 'Montgomery'
            });

    App.cardsController.addCard(card1);
};

App.Card = Ember.Object.extend({
    id: null,
    title: null,
    desc: null,
    current: true
});

App.cardsController = Ember.ArrayController.extend({
    content: [],

    //Property that adds an item to content
    addCard: function(item){
        this.addObject(item);
    }
});

2 个答案:

答案 0 :(得分:2)

假设这是构成应用程序的唯一代码,您需要在Application create语句之后直接定义控制器对象,因此before按顺序使用它:

App = Ember.Application.create({
  ready: function(){
    //Populate content[] in cardController
    App.GetCards();
  }
});

App.Card = Ember.Object.extend({
  id: null,
  title: null,
  desc: null,
  current: true
});

App.cardsController = Ember.ArrayController.create({
  content: [],

  //Property that adds an item to content
  addCard: function(item){
    this.addObject(item);
  }
});

App.GetCards = function(){
  card1 = App.Card.create({
            id: 0,
            title: 'Alabama',
            desc: 'Montgomery'
        });

  App.cardsController.addCard(card1);
};

仅仅为了证明而工作fiddle

答案 1 :(得分:0)

我正在研究一个有这段代码的旧Ember.js教程:

App.recentUsersController = Ember.ArrayController.create({
    content: [],
    addUser: function(name) {
        if( this.contains(name) ) this.removeObject(name);
        this.pushObject(name);
    },
    removeUser: function(view) {
        this.removeObject(view.context);
    },
    searchAgain: function(view) {
        App.tweetsController.set('username', view.context);
        App.tweetsController.loadTweets();
    },
    reverse: function(){
        return this.toArray().reverse();
    }.property('@each')
});

我不断收到错误Error: Assertion Failed: Ember.Object.create no longer supports defining computed properties. Define computed properties using extend() or reopen() before calling create().

好吧,我采用了非常具有描述性(且有用)的错误信息,并将其与我对链接的了解相结合,我得到了这个:

App.recentUsersController = Ember.ArrayController.extend({
    content: [],
    addUser: function(name) {
        if( this.contains(name) ) this.removeObject(name);
        this.pushObject(name);
    },
    removeUser: function(view) {
        this.removeObject(view.context);
    },
    searchAgain: function(view) {
        App.tweetsController.set('username', view.context);
        App.tweetsController.loadTweets();
    },
    reverse: function(){
        return this.toArray().reverse();
    }.property('@each')
}).create({});

首先扩展,然后创建。为我工作,并希望把它扔出去,以防其他人遇到这个问题与老版本的Ember一起使用。