【发布时间】:2014-11-25 08:38:59
【问题描述】:
Trucks 模型由 db 支持,并可通过 rest api 访问。 然而,分析模型不受数据库支持,而只是用于客户端的计算。
我需要从 Analyses 控制器中访问 Trucks 数组,但遇到了 2 个问题
需要从数据库中检索卡车数据。如果我立即访问
/analyses路线,那么在检查 ember 控制台时数据存储中没有卡车。但是,如果我先访问/trucks,我会注意到检索到 6 条记录并在数据存储中。在
AnalysesController中,我已经指定了对卡车的依赖,但我无法从控制器中访问数据。 http://emberjs.com/guides/controllers/dependencies-between-controllers/ 展示了如何从模板中访问。
下面是代码
// router.js
Classmove.Router.map(function() {
this.resource('trucks');
return this.resource('analyses');
});
Classmove.TrucksRoute = Ember.Route.extend({
model: function() {
return this.store.find('truck');
}
});
Classmove.Analyses = Ember.Route.extend({
model: function() {
return this.store.find('analysis');
}
});
// analyses_controller.js
Classmove.AnalysesController = Ember.ArrayController.extend({
needs: ['trucks'],
trucks: Ember.computed.alias('controllers.trucks'),
isCalculating: false,
checkTrucks: function() {
// I want to access the trucks here
// I found that I was able to access it like so
this.get('trucks').model.content
// but shouldn't there be a direct way without bypassing Ember
},
actions: {
calculate: function() {
// does stuff - removed from example for simplicity
}
}
});
【问题讨论】:
-
您应该使用 get 方法来访问属性。而不是
this.get('trucks').model.content使用this.get('trucks.model')并且不要忘记checkTrucks方法上的observes('trucks.model)
标签: ember.js ember-data