2

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

车把-1.0.0-rc.3.js、
ember-1.0.0-rc.3.js、
ember-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);
    }
});
4

2 回答 2

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

工作小提琴只是为了证明。

于 2013-05-14T13:57:49.377 回答
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 时遇到这个问题。

于 2014-04-23T19:03:06.303 回答