0
所以我正在使用Ember.js在XMPP客户端上工作。由于我的数据来自XMPP,我想创建自己的模型,并找到了这个很好的教程:http://eviltrout.com/2013/03/23/ember-without-data.html和小示例应用程序emberreddit。当使用find时,不会在对象上调用init
设置应该很简单。我只是延长Ember.Object并实现了查找功能,无论是创建或返回对象:
App.Conversation = Ember.Object.extend({
messages: [],
talkingPartner: null,
init: function(){
this._super();
console.log("Init called for App.Conversation");
//Binding for XMPP client event
$.subscribe('message.client.im', _.bind(this._onMessage, this));
},
//Private Callbacks
_onMessage: function(event, message){
console.log("Received message");
this.find(message.jid).messages.pushObject(message);
}
});
App.Conversation = Ember.Object.reopenClass({
store: {},
find: function(id){
if(!this.store[id]){
this.store[id] = App.Conversation.create();
}
return this.store[id];
}
});
这大致遵循从here的代码。它工作正常,但不会调用init
。如果我创建的对象不使用find
它的作品。所以我有点困惑。
- 据我所知
store
应该为App.Conversation
所有实例相同。那是对的吗?此外,如果是真的,我不得不 移动messages
和talkingPartner
到init
并通过this.set('message')
设置它们,不要一 - 为什么
ìnit
当App.Conversateion.create()
被称为App.Conversation.find(id)
不叫。谁能解释为什么?我发现Ember.js的表现有时与一开始预期的有所不同。
非常感谢!我想这对我来说太明显了。 – Karsten 2013-04-12 14:00:21