1

我有一个人员类,我想跟踪在我的应用程序中创建的所有人员实例。

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

在创建对象后是否有任何钩子被执行?

4

1 回答 1

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

于 2013-10-31T12:35:20.773 回答