2013-05-14 106 views
2

我是新來的燼,我想加載一個小陣列到控制器。麻煩的是,我在cardsController中定義的addCard函數沒有顯示出來,並且出現錯誤:「object function()沒有方法'addCard'」。我究竟做錯了什麼?我使用了以下內容:Ember Array控制器 - 函數未加載

車把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

假設是是讓你的應用程序的唯一代碼,您需要定義應用程序之後,控制器對象直接創建語句,因此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); 
}; 

Working fiddle只是爲了證明。

+0

我剛發現我的錯誤。當創建cardsController時,我試圖擴展cardsController而不是實際創建它 – jonesy 2013-05-14 14:06:23

+0

感謝您指出它,修復它在未來人的答案:) – intuitivepixel 2013-05-14 14:11:45

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一起工作。