【问题标题】:Route returns empty model when using find() methodRoute 在使用 find() 方法时返回空模型
【发布时间】:2014-05-18 21:38:07
【问题描述】:

我有一个包含一些用户设置的“时钟”记录。记录保存在本地存储中。如果页面被刷新,我不想在已有记录的情况下一直创建新记录。
我正在使用 Ember 数据和 LSAdapter。

我的方法:
在 ApplicationRoute 中,我检查是否存在“时钟”类型的记录。

如果是 --> 将 firstObject 作为模型返回。
如果没有 --> 创建新记录并返回它

始终创建新记录并返回它的简单案例可以正常工作。唱片成为我的榜样。但是,一旦我使用 this.find() ,路由就会返回一个空模型。

App.ApplicationRoute = Ember.Route.extend({

    model : function(){
        var length, clock;
        var self = this;

        this.store.find('clock').then(function(record){

            length = record.get("length");  // works

            if(length == 0){
                clock = self.store.createRecord('clock', {
                    soundOgg: "data/sounds/cling.ogg",
                    soundMp3: "data/sounds/cling.mp3"
                });
                console.log(clock); // prints correct object

                return clock;   // returns empty model

            } else {
                clock = record.get('firstObject');
                console.log(clock); // prints correct object

                return clock;   // returns empty model
            }    
        });    
    }
});

为什么它不起作用?有没有更好的方法来返回正确的模型?

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    而不是这个:

    this.store.find('clock').then(...
    

    这样做:

    return this.store.find('clock').then(...
    

    路由器的model 钩子应该return 一个承诺。

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多