将新数据添加到商店时无法更新模板

时间:2013-09-19 22:00:24

标签: ember.js ember-data

我试图弄清楚如何使用ember数据正确执行保存。我的模型定义为:

AS.AnalyticsRun = DS.Model.extend({
'exerciseId'                : DS.attr('number'),
'exerciseRunId'             : DS.attr('number'),
'queued'                    : DS.attr('boolean'),
'final'                     : DS.attr('boolean'),
'analyticsRunParameters'    : DS.hasMany('analyticsRunParameter')
});


AS.AnalyticsRunParameter = DS.Model.extend({
'name'                  : DS.attr('string'),
'category'              : DS.attr('string'),
'filteredOut'           : DS.attr('boolean', { 'default' : false }),
'equalsDefaultValue'    : function() {
    /* computed property : returns css class name that should be used for the row with parameter having value other than default one*/
    return this.get('value') == this.get('default');
}.property('value', 'default'),
'analyticsRun'          : DS.belongsTo('analyticsRun')

});

在我的控制器中,我有一个执行插入的功能:

 createNewAnalyticsRun : function(createRunFrom, createRunFromId){
    var self = this;

    $.ajax({
        type: "POST",
        url: AS.baseURL+"analyticsRuns",
        data: {"createFrom": createRunFrom, "id" : createRunFromId, "exerciseId" : AS.Analytics.get('exerciseId'), "exerciseRunId" : AS.Analytics.get('exerciseRunId')},
        success: function(newAnalyticsRun){
            self.get('store').push('analyticsRun', newAnalyticsRun.analyticsRun);
            for(var i = 0, len = newAnalyticsRun.analyticsRunParameters.length; i<len; i++){
                self.get('store').push('analyticsRunParameter', newAnalyticsRun.analyticsRunParameters[i]);
            }
        },
        dataType: "JSON"
    });

}

我的路线如下:

AS.AnalyticsRunsIndexRoute = Ember.Route.extend({
model : function() {
    var store = this.get('store');
    return store.find('analyticsRun',{'exerciseId':1});
}

});

当我的路线设置为:

时,此设置可以完美地工作(使用保存时添加的数据更新我的模板)
 return store.find('analyticsRun'); 

但是

return store.find('analyticsRun',{'exerciseId':1});

即使添加的记录的exerciseId为1,我的模板也不会在保存时更新。当我刷新页面时,会显示新添加的数据。我很确定在查找中添加过滤器会导致问题。

解决方案:我最终使用jquery ajax调用来休息api以使数据与@ sly7_7提供的解决方案保持一致,就像这样

AS.AnalyticsRunsIndexRoute = Ember.Route.extend({
model : function() {
    var store = this.get('store'), self = this;

    var exerciseId = //get exercise id from some other logic

    $.ajax({
        type: "GET",
        url: AS.baseURL+"analyticsRuns",
        data: {"exerciseId" : exerciseId},
        success: function(analyticsRunData){
            for(var i = 0, len = analyticsRunData.analyticsRuns.length; i<len; i++){
                self.get('store').push('analyticsRun', analyticsRunData.analyticsRuns[i]);
            }
            for(i = 0, len = analyticsRunData.analyticsRunParameters.length; i<len; i++){
                self.get('store').push('analyticsRunParameter', analyticsRunData.analyticsRunParameters[i]);
            }
        },
        dataType: "JSON"
    });

    //perfect since I need to return model anyways without calling find
    return store.filter('analyticsRun', function(analyticRun){
        return analyticRun.get('exerciseId') == exerciseId;
    });
},
setupController : function(controller,model){
    this._super(controller,model);
    controller.set('analyticsRunSelectOptions',controller.initAnalyticsRunSelectOptions());
    this.controllerFor('analysisTemplates').set('model',controller.get('store').find('analysisTemplate'));
}
});

1 个答案:

答案 0 :(得分:1)

您是否尝试使用store.filter而不是store.find?如果你想要的是过滤并将结果保持为实时数组,那么过滤就是最佳选择。 在模型钩子中,我会写一些类似的东西:

AS.AnalyticsRunsIndexRoute = Ember.Route.extend({
  model : function() {
    store.find('analyticsRun', {exerciseId: 1});
    return store.filter('analyticsRun', function(analyticRun){
      return analyticRun.get('exerciseId') == 1;
    });
  }

});