2013-10-31 89 views
1

我有一個人類&我想跟蹤在我的應用程序中創建的所有人員實例。跟蹤已創建實例的列表

var people = []; 
// My failed attempt 
App.Person = Ember.Object.extend({ 
    create: function(){ 
    var instance = this._super(); 
    people.push(instance); 
    return instance; 
    } 
}) 

是否有任何掛鉤在創建對象後執行?

回答

3

我想你要找的是Ember.Object.init

var people = []; 
App.Person = Ember.Object.extend({ 
    init: function() { 
    people.push(this); 
    } 
}); 

var sophia = App.Person.create({name: "Sophia"}); 
var greta = App.Person.create({name: "Greta"}); 

var names = people.getEach('name'); 

http://emberjs.com/api/classes/Ember.Object.html#method_init

+0

我怎麼給忘了** **初始化!我需要一些嚴肅的睡眠:P謝謝:) –

相關問題