2012-10-23 82 views
0

我使用Ember Data来定义两个模型以及两者之间的关联,然后使用createRecord方法插入一些数据。不涉及关联的属性存在于模型和gettable中(它们返回适当的值),但对关联内容的调用返回一个空数组。我究竟做错了什么?createRecord没有填充关联

编辑:因此,使用load代替createRecord现在给[3,3]数组,我仍然无法访问的属性。我能得到的最接近的是:b.get("messages").get("firstObject").id,它返回字符串“[对象的对象]”

// Store 

App.store = DS.Store.create({ 
    revision: 6, 
    adapter: App.adapter 
}); 

// Models 

App.Message = DS.Model.extend({ 
    msg: DS.attr('string'), 
    time: DS.attr('string'), 
    chat: DS.belongsTo('App.Chat') 
}); 

App.Chat = DS.Model.extend({ 
    name: DS.attr('string'), 
    avatar: DS.attr('string'), 
    messages: DS.hasMany('App.Message', { embedded: true }), 
    preview: function() { 
     this.get('messages').firstObject; 
    }.property('messages') 
}); 

var a = App.store.createRecord(App.Chat, { "id": 1, 
              "name": 'Foo Bar', 
              "avatar": 'test', 
              "messages": [{ 
               "id": 1, 
               "msg": 'This is a test post one two three', 
               "time": '1:10pm' 
              },{ 
               "id": 2, 
               "msg": ' This is another test post three two one', 
               "time": '1:15pm' 
              }] 
             }); 

var b = App.store.find(App.Chat, 1); 
console.log(b.get("name")) // Foo Bar 
console.log(b.get("messages").get("content")) // [] (an empty array) 

回答

0

貌似支持{embedded: true}是不是太不久前取出,这就是为什么这不工作的原因。有一个正在等待的pull请求(有效的)将这个功能恢复。

https://github.com/emberjs/data/pull/428