Emberjs findRecord 然后不等待解决
Emberjs findRecord then not waiting for resolve
我有一个符合 json api 约定的 findRecord 方法,如下所示:
this.store.findRecord('book', 2).then((book) =>{
console.log(book.get('name'));
});
控制台日志一直在打印undefined
,但实际上正在发送名称值。 book
只有 returns id
仅此而已。我用 Ember.$ ajax 进行了测试,我得到了一切。模型结构也正确。
这是正常行为吗?这让我发疯了几个小时......
谢谢。
编辑:
工作片段:
Ember.$.getJSON('http://api.xxxxx.com/v1/books/' + id).then((book) => {
console.log(book.get('name'));
});
编辑2:
图书型号:
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';
export default Model.extend({
name: attr('string'),
year: attr('number'),
authors: hasMany('author')
});
If { reload: true } is passed or adapter.shouldReloadRecord evaluates to true, then the returned promise resolves once the adapter returns data, regardless if the requested record is already in the store:
this.store.findRecord('book', 2, {reload: true}).then((book) =>{
console.log(book.get('name'));
});
参考:https://emberjs.com/api/ember-data/2.16/classes/DS.Store/methods/findRecord?anchor=findRecord
我有一个符合 json api 约定的 findRecord 方法,如下所示:
this.store.findRecord('book', 2).then((book) =>{
console.log(book.get('name'));
});
控制台日志一直在打印undefined
,但实际上正在发送名称值。 book
只有 returns id
仅此而已。我用 Ember.$ ajax 进行了测试,我得到了一切。模型结构也正确。
这是正常行为吗?这让我发疯了几个小时...... 谢谢。
编辑:
工作片段:
Ember.$.getJSON('http://api.xxxxx.com/v1/books/' + id).then((book) => {
console.log(book.get('name'));
});
编辑2:
图书型号:
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';
export default Model.extend({
name: attr('string'),
year: attr('number'),
authors: hasMany('author')
});
If { reload: true } is passed or adapter.shouldReloadRecord evaluates to true, then the returned promise resolves once the adapter returns data, regardless if the requested record is already in the store:
this.store.findRecord('book', 2, {reload: true}).then((book) =>{
console.log(book.get('name'));
});
参考:https://emberjs.com/api/ember-data/2.16/classes/DS.Store/methods/findRecord?anchor=findRecord