【发布时间】:2015-05-24 12:43:12
【问题描述】:
由于超出此问题范围的原因,我必须在我的 SearchRoute 中使用 Ember.$.getJSON 填充名为 Activity 的 Ember 数据模型,例如这个:
App.SearchRoute = Ember.Route.extend({
model: function (params) {
// Create a promise to return to the model hook. The promise will return a DS.RecordArray.
var modelPromise = new Ember.RSVP.Promise(function (resolve, reject) {
// Make the AJAX call to retrieve activities that match the search criteria
Ember.$.getJSON('/services/activities?query=' + params.q).then(function (data) {
data.activities.forEach(function (activity) {
// If the current activity does not already exist in the store...
if (!this.store.hasRecordForId('activity', activity.id)) {
// add the activity to the store
this.store.createRecord('activity', {
id: activity.id,
title: activity.propertyBag.title
});
}
}.bind(this));
resolve(this.store.all('activity', { query: params.q }));
}.bind(this));
}.bind(this));
// Return the DS.RecordArray as the model for the search route
return modelPromise;
}
});
然后,在我的 SearchController 中,我在从绑定到显示结果的模板的计算属性返回结果之前进行一些模型排序和过滤,如下所示:
App.SearchController = Ember.ArrayController.extend({
filteredActivities: function () {
var model = this.get('model');
// complete various model sorting and filtering operations
return model;
}.property('model')
});
这是模板:
<script type="text/x-handlebars" data-template-name="activities">
{{#each item in filteredActivities}}
{{item.title}}
{{/each}}
</script>
每次执行搜索时,SearchRoute 中的模型挂钩都会刷新,发出 AJAX 请求,并使用新的 Activity 记录更新存储,如果必要的。
问题是,即使我确实使用 createRecord 在商店中创建了新记录并将新的商店查询结果返回到我的模型挂钩,filteredActivities 属性也不会被触发并且模板也不会更新。
我认为因为我将新更新的 DS.RecordArray 返回到模型挂钩,Ember 会认为我的模型已更改并触发任何计算属性以监视模型的更改,但我一定遗漏了一些东西.
有人有什么想法吗?
很抱歉发了这么长的帖子,非常感谢您花时间考虑我的问题!
【问题讨论】:
标签: ember.js ember-data