我有一个应用程序,它允许我创建一条可以发送到 N 个社交网络的消息,建模如下:
Social.Account = DS.Model.extend({
    username: DS.attr("string"),
    messages: DS.hasMany("Social.Message")
});
Social.Message = DS.Model.extend({
    text: DS.attr("string"),
    account: DS.belongsTo("Social.Account")
});
我创建记录的代码如下所示:
saveMessage: function(){
    var account = Social.Account.find(this.get("id")),
        msg = Social.store.createRecord(
                  Social.Message,
                  {
                      text: postingWindow.get('text'),
                      account: account,
                  }
              );
    account.get("messages").addObject(msg);
    Social.store.commit();
}
表单数据如下所示:
text:testing one three five seven
message_key:
tags:
user_id:
created:Wed, 19 Jun 2013 21:39:14 GMT
scheduled_at:2013-06-20T03:00:00.000Z
is_editing:false
status:C
account:56
那部分效果很好。现在我们正在计划未来的消息,这为用户提供了在保存之后但在发送之前编辑它们的机会。从表面上看,我的实现似乎工作正常,但我注意到 Ember 没有将帐户信息与帖子一起发送,这意味着当我的服务器代码运行时,该消息在数据库中被孤立了。这是更新代码:
sendNow: function(message){
    var account = Social.Account.find( this.get('controllers.account.id') );
    message.setProperties({
        scheduled_at: '',
        accounts: [account]
        // I've also tried accounts: this.get('controllers.account.id')
    });
    Social.store.commit();
}
以及对应的表单数据:
text:testing one three five seven
message_key:
tags:
user_id:17
created:Wed, 19 Jun 2013 21:39:14 GMT
scheduled_at:
is_editing:false
无论我做什么,Ember 都不会将帐户数据发送到服务器。是否有更合适的方法来更新记录并使其正确保存?