一个简单的问题,我可以从 ember 中的 mixin 中获取对控制器的引用吗?
我有一个 ApplicationController,我在其中设置通过 ember-data 检索的值,我想从 Mixin 中引用这些值。我找不到任何东西,所以我的假设是没有用于此的 API?
App.Searchable = Ember.Mixin.create({
search: function () {
if (this.isValid()) {
var controller = this.controllerFor('application');
return controller.getProperties('fromDate', 'toDate', 'brokers', 'locations');
}
else
return this.isInvalid;
},
isValid: function () {
var controller = this.controllerFor('search');
var fromDate = moment(this.get('fromDate'), 'YYYY-MM-DD');
var toDate = moment(this.get('toDate'), 'YYYY-MM-DD');
return moment(fromDate).isBefore(toDate) || moment(fromDate).isSame(toDate);
},
isInvalid: function () {
$("#date-range-error").show();
}
});
App.PnlRoute = Ember.Route.extend(App.Searchable, {
model: function () {
return this.store.find('pnl', this.search());
},
renderTemplate: function () {
this.render('pnl', {
into: 'application',
outlet: 'data',
controller: 'pnl'
});
},
actions: {
filter: function () {
this.controllerFor('pnl').set('model', this.store.find('pnl', this.search()));
}
}
});
App.ApplicationController = Ember.ArrayController.extend({
title: 'Pnl',
fromDate: null,
toDate: null,
locations: [],
ccyPairs: [],
brokers: [],
init: function () {
var self = this;
this.store.find('date').then(function (result) {
var date = result.objectAt(0);
self.set('fromDate', date.get('from'));
self.set('toDate', date.get('to'));
}), function (error) {
$("#date-init-error").show();
};
}
});