Ember.js 当使用find时,不会对对象调用init

Ember.js 当使用find时,不会对对象调用init,ember.js,Ember.js,因此,我正在使用Ember.js开发一个XMPP客户机。因为我的数据来自XMPP,所以我想创建自己的模型,并找到了这个不错的教程:以及这个小示例应用程序 设置应该非常简单。我只是扩展了Ember.Object并实现了一个find函数,该函数创建或返回对象: App.Conversation = Ember.Object.extend({ messages: [], talkingPartner: null, init: function(){ this.

因此,我正在使用Ember.js开发一个XMPP客户机。因为我的数据来自XMPP,所以我想创建自己的模型,并找到了这个不错的教程:以及这个小示例应用程序

设置应该非常简单。我只是扩展了Ember.Object并实现了一个find函数,该函数创建或返回对象:

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];
    }
});
这大致遵循的代码来自。它工作正常,但从未调用
init
。如果我不使用
find
创建对象,它会工作。所以我有点困惑

  • 据我所知,
    store
    对于
    App.Conversation
    。对吗?而且,如果这是真的,我必须 将
    消息
    通话伙伴
    移动到
    init
    并通过
    this.set('message')
    ,不需要我
  • 为什么在
    App.Conversation.find(id)
    中调用
    App.Conversation.create()
    时不调用
    。有人能解释为什么吗?我发现Ember.js有时的行为与最初的预期略有不同

您需要更改以下内容:

App.Conversation = Ember.Object.reopenClass({
为此:

App.Conversation.reopenClass({
您的代码正在重新打开Ember.Object本身,并且将完全覆盖
App.Conversation
的定义


这是一个基于您的代码的示例。

非常感谢!我想这对我来说太明显了。