Reputation: 131
A simple question, can I get a reference to a controller from a mixin in ember?
I have an ApplicationController where I set values retrieved via ember-data, I want to reference these values from a Mixin. I could not find anything so my assumption is there is no API for this?
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();
};
}
});
Upvotes: 1
Views: 1219
Reputation: 23322
Using this.controllerFor('application')
inside the mixin does not work, therefore one way you could do that is to store a reference of the ApplicationController
in a local variable in the Mixin you create, so you will later have access to it easily.
For example:
App.ApplicationController = Ember.ObjectController.extend();
App.Searchable = Ember.Mixin.create({
ctrl: null,
search: function() {
console.log(this.get('ctrl'));
}
});
App.ApplicationRoute = Ember.Route.extend(App.Searchable, {
activate: function() {
this.set('ctrl', this.controllerFor('application'));
this.search();
}
});
Example jsbin: http://jsbin.com/ILeguSO/4/edit
Hope it helps.
Upvotes: 1