在我的应用中,用户可以输入朋友的描述或推荐已经存在的描述。这两种方法(createDescription和upvoteDescription)都在数据库中保留。 upvoteDescription更改DOM,但createDescription不会。这可能是因为我在模型中传递了一个参数,但我无法解决这个问题 - api需要它。
//descriptions route
App.DescriptionsRoute = Ember.Route.extend({
...
model: function () {
var store = this.get('store'),
friend = this.modelFor('friend');
return store.find('description', {friend_id: friend.id});
}
})
//descriptions controller
App.DescriptionsController = Ember.ArrayController.extend({
...
actions: {
createDescription: function () {
var name = this.get('name'),
friend_id = this.get('controllers.friend').get('id'),
store = this.get('store'),
description = store.createRecord('description', {
name: name,
friend_id: friend_id
});
description.save();
},
upvoteDescription: function (description) {
var count = description.get('count');
description.set('count', count + 1);
description.save();
}
}
});
//descriptions template
{{input value=name action="createDescription" type="text"}}
{{#each controller}}
<div {{bind-attr data-name=name data-count=count }} {{action upvoteDescription this}}>
<div>{{name}}</div>
<span>{{count}}</span>
</div>
{{/each}}
答案 0 :(得分:4)
find(通过查询)不会主动确保它具有与查询匹配的记录,因此您必须手动将其注入结果中。
createDescription: function () {
var name = this.get('name'),
friend_id = this.get('controllers.friend').get('id'),
store = this.get('store'),
description = store.createRecord('description', {
name: name,
friend_id: friend_id
});
description.save();
this.pushObject(description);
},
或者您可以使用实时记录数组(filter
/ all
)
model: function () {
var store = this.get('store'),
friend = this.modelFor('friend');
store.find('description', {friend_id: friend.id});
return store.filter('description', function(record){
return record.get('friend_id') == friend.id;
});
}