Ember: 如何获取在 Route->Model 处返回的文件的值
Ember: How to get values of a file returned at Route->Model
import Route from '@ember/routing/route';
export default Route.extend({
model() {
return this.get('store').findAll('rentals');
}
});
上面的代码 returns 所有租赁,比如说 10 个对象。现在,我想获得第一个、最后一个,甚至多个,但做不到。尝试过:
return this.get('store').findAll('rentals')[0];
return this.get('store').findAll('rentals').get(0);
return this.get('store').findAll('rentals').firstObject;
return this.get('store').findAll('rentals').slice(0, 2);
return this.get('store').findAll('rentals').splice(0, 2);
您尝试在哪里执行此代码?在控制器中?然后 this.get('model.firstObject')
应该工作。
在其他情况下 - 它可能不会像 findAll
returns 那样承诺。所以你一定要等待这个promise resolve,例如:
this.get('store').findAll('rentals').then(loadedRentals => {
// Here we have access to 'rental' objects
loadedRentals.forEach(r => console.log(`Rental ${r} is fully loaded`));
}
import Route from '@ember/routing/route';
export default Route.extend({
model() {
return this.get('store').findAll('rentals');
}
});
上面的代码 returns 所有租赁,比如说 10 个对象。现在,我想获得第一个、最后一个,甚至多个,但做不到。尝试过:
return this.get('store').findAll('rentals')[0];
return this.get('store').findAll('rentals').get(0);
return this.get('store').findAll('rentals').firstObject;
return this.get('store').findAll('rentals').slice(0, 2);
return this.get('store').findAll('rentals').splice(0, 2);
您尝试在哪里执行此代码?在控制器中?然后 this.get('model.firstObject')
应该工作。
在其他情况下 - 它可能不会像 findAll
returns 那样承诺。所以你一定要等待这个promise resolve,例如:
this.get('store').findAll('rentals').then(loadedRentals => {
// Here we have access to 'rental' objects
loadedRentals.forEach(r => console.log(`Rental ${r} is fully loaded`));
}