Ember模型未更新 [英] Ember model not updated
问题描述
当我创建一个新的挑战时,我有一条路线显示挑战,但是记录持续存在,但是当我转换回路由时,挑战列表的模型没有被更新。有没有我缺少的东西?
I have a route that displays a list of challenges, when I create a new challenge the record is persisted however the model of list of challenges is not updated when I transition back into the route. Is there something I am missing?
//new.js
var challenge = this.store.createRecord('challenge', {
name_en: this.get('model.name_en'),
name_fr: this.get('model.name_fr'),
description_en: this.get('model.description_en'),
description_fr: this.get('model.description_fr'),
end_date: this.get('model.end_date'),
start_date: this.get('model.start_date'),
points_cap: this.get('model.points_cap'),
points_goal: this.get('model.points_goal'),
challenge_type: 1,
short_description_en: this.get('model.short_description_en'),
short_description_fr: this.get('model.short_description_fr'),
excluded_activities: excluded
});
// Persist record.
challenge.save().then((challenge) => {
this.transitionToRoute('challenges');
}).catch((error) => {
this.handleError(error, 'error.system_error');
});
//router.js
Router.map(function() {
this.route('challenges', function() {
this.route('new');
this.route('challenge', {
path: ':challenge_id'
}, function() {
this.route('delete');
this.route('edit');
});
} );
//challenges.js
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
import UserProfile from '../models/user-profile';
export default Ember.Route.extend(AuthenticatedRouteMixin,{
userProfile: UserProfile.create(),
model: function() {
return this.store.query('challenge', {league_id: this.get('userProfile.league_id')});
}
}) p>
});
//new challenge payload
{
"activity_exclusion_list":[
],
"challenge_type":1,
"challengeUrl":null,
"end_date":"31-10-2015",
"number_participants":null,
"number_teams":null,
"points_cap":null,
"points_goal":null,
"start_date":"01-10-2015",
"leagueId":"1",
"teams":[
],
"name_lang":{
"en":"New Challenge ",
"fr":null
},
"description_lang":{
"en":"New Challenge",
"fr":null
},
"short_description_lang":{
"en":"New Challenge",
"fr":null
}
}
//response from new challenge
{
"challenge_type":"Individual",
"description":" ",
"description_fr":null,
"description_lang":{
"en":"New Challenge",
"fr":null
},
"challengeUrl":" ",
"start_date":"01-10-2015",
"end_date":"31-10-2015",
"name":" ",
"name_fr":null,
"name_lang":{
"en":"New Challenge ",
"fr":null
},
"points_cap":0,
"points_goal":0,
"short_description":" ",
"short_description_fr":null,
"short_description_lang":{
"en":"New Challenge",
"fr":null
},
"number_participants":0,
"number_teams":0,
"teams":[
],
"challenge_id":265,
"activity_exclusion_list":[
],
"leagueId":1
}
推荐答案
您尝试使用 this.store.filter
?问题可能是查询函数只返回一个RecordArray,而过滤器返回一个Live RecordArray,当解决承诺(在这种情况下是成功的保存))时,它将更新您的模板和其他所有内容。
In your challenges route have you tried using a this.store.filter
instead? The issue could be that the query function only returns a RecordArray, whereas filter returns a Live RecordArray which will update your templates and everything else when the promise (in this case the successful save) is resolved.
model: function() {
return this.store.filter('challenge', {league_id: this.get('userProfile.league_id')}, function() {
// We're not actually filtering, so just return true for everything
return true;
});
}
我有同样的问题,原来是解决方案,希望它帮助!
I had the same problem and this turned out to be the solution, hope it helps!
这篇关于Ember模型未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!