开发允许帐户向社交媒体发布消息的应用程序。我正在尝试将我的第一条消息保存到 Ember 数据存储区,但它似乎不起作用。问题是它也没有出错,所以我不确定发生了什么。这是设置:
楷模
Social.Account = DS.Model.extend({
username: DS.attr('string'),
messages: DS.hasMany('Social.Message')
});
Social.Message = DS.Model.extend({
user_id: DS.attr('number'),
text: DS.attr('string'),
accounts: DS.hasMany('Social.Account'),
posts: DS.hasMany('Social.Post')
});
Social.Post = DS.Model.extend({
created: DS.attr('date'),
text: DS.attr('string'),
message: DS.belongsTo('Social.Message')
});
数据
Social.Account.FIXTURES = [{
id: 1,
username: "commadelimited",
messages: [1, 2]
}];
Social.Message.FIXTURES = [{
id: 1,
user_id: 1,
text: 'This is message #1 sent by account #1',
accounts: [1],
posts: [1]
}, {
id: 2,
user_id: 1,
text: 'This is message #2 sent by account #1',
accounts: [1],
posts: [2]
}];
Social.Post.FIXTURES = [{
id: 1,
created: "5 minutes ago",
text: 'This is post #1 sent by account #1, tied to message #1'
}, {
id: 1,
created: "5 minutes ago",
text: 'This is post #2 sent by account #1, tied to message #2'
}];
在 Social.NewPostView 的上下文中,我有一个调用提交方法的按钮
Social.NewPostView = Ember.View.extend({
tagName: 'div',
submit: function(e) {
msg = Social.Message.createRecord({
id: 1,
user_id: 1,
text: 'This was a created message',
created: "2013-02-27 13:09:22",
accounts: [1],
posts: [1]
});
console.log('done');
}
});
当用户单击按钮时,我可以触发一个警报框,所以我知道该方法正在触发。你可以看到我在这里硬编码了一个 createRecord 调用(但在应用程序中我使用的是用户输入的数据)。日志语句显示在控制台中,但我没有看到预期的 UI 更改。此外,当我自省用户 1 的帐户对象时,我得到 0 长度:
Social.Account.find().objectAt(0).get('messages').get('length')
当我在控制台中运行此语句时,我确实得到了一些显示:
Social.Message.createRecord({ user_id: 1, text: 'This was a created message', created: "2013-02-27 13:09:22", accounts: [1], posts: [1] })
但它是一个复杂的对象,我不确定它实际上是什么。我的代码基于@toranb 的complex-ember-data-example项目,它似乎具有可比性。我确定我做错了什么,但我不知道它是什么。任何输入都会被重视。
更新 1
通过Social.Message.find().get('length')
在提交之前记录我得到 0。如果我点击提交,然后立即运行相同的日志语句,我得到以下内容:
4
Uncaught Error: Attempted to handle event `loadedData` on <Social.Message:ember427:1> while in state rootState.loaded.created.uncommitted. Called with undefined
这表明消息确实正在创建,但未显示在 UI 中。我的下一步可能是什么?