我有一个人课和一个我想跟踪在我的应用程序中创建的所有人员实例。
var people = [];
// My failed attempt
App.Person = Ember.Object.extend({
create: function(){
var instance = this._super();
people.push(instance);
return instance;
}
})
在创建Object之后是否有任何钩子被执行?
答案 0 :(得分: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