路由代码运行和模板渲染之间的Ember模型的日期属性变为空

时间:2014-08-19 22:24:48

标签: javascript ember.js ember-data

我无法在我的Ember应用程序中保存模型上的日期属性并让它们坚持下去。我的模型的编辑页面(dataset.edit)有一个保存操作,应该将模型的updatedAt属性设置为当前时间,保存模型,然后转换到显示模型的新属性的页面(dataset.index) 。保存操作中的记录代码显示此时正确设置了日期值(尽管即使在调用model.save()后,Ember检查器也会报告日期属性为空)。

当应用转换为dataset.index以向用户显示模型的新属性时,模型的日期在路径的model挂钩中正确设置,但是当模板被渲染到全部时模型的日期属性变为null!任何从模板或控制器获取日期属性的尝试只会看到空值...即使在路径的model挂钩运行时正确设置了相同的日期属性。怎么可能?

这是我的代码的相关部分:

DataGatherer.ApplicationSerializer = DS.LSSerializer.extend();
DataGatherer.ApplicationAdapter = DS.LSAdapter.extend({
    namespace: 'DataGatherer'
});


App.Dataset = DS.Model.extend({

    // The time at which this model was last updated.
    updatedAt: DS.attr('date')
});


App.DatasetEditRoute = Ember.Route.extend({
    model: function() {
        return this.modelFor('dataset');
    }
});

App.DatasetEditController = Ember.ObjectController.extend({
    actions: {
        save: function() {
            var self = this;

            var dataset = this.get('model');
            dataset.set('updatedAt', new Date());
            // DEBUG
            console.log("After setting Dataset.updatedAt:");
            console.log(dataset.get('updatedAt'));  // correctly prints the date
            }
            dataset.save().then(function() {
                // DEBUG
                console.log("After saving:");
                console.log(dataset.get('updatedAt'));  // correctly prints the date
                self.transitionToRoute('dataset.index', dataset);
            });
        }
});


App.DatasetRoute = Ember.Route.extend({
    model: function(params){
        return this.store.find('dataset', params.dataset_id);
    }
});


App.DatasetIndexRoute = Ember.Route.extend({
    model: function() {
        // DEBUG
        var model = this.modelFor('dataset');
        console.log('After loading new route:');
        console.log(model.get('updatedAt'));  // correctly prints the date

        return this.modelFor('dataset');
    }
});


App.DatasetIndexController = Ember.ObjectController.extend({
     actions: {
        whatsTheDate: function() {
            // DEBUG
            console.log("Dataset.updatedAt:");
            console.log(this.get('model').get('updatedAt'));  // when invoked prints null!
         }
     }
});

0 个答案:

没有答案