【发布时间】: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