Users in my app have profile pages. User can view his own profile and profiles of other users. On his own profile he is able to change his avatar, but of course he can't change avatar of another user. Script for avatar uploading is loaded on demand, using profile route's beforeModel hook. The problem is to load this script only when user visits his own profile, not profiles of other users.
Here is my code:
Router:
Router.map(function () {
this.resource('user', { path: '/:user_id' }, function () {
this.resource('profile');
});
});
Application route:
var ApplicationRoute = Em.Route.extend({
model: function () {
return {
currentUser: this.config.currentUser
};
}
});
User route:
var UserRoute = Em.Route.extend({
model: function (params) {
var currentUser = this.modelFor('application').get('currentUser');
if (currentUser.get('id') === params.user_id) {
return currentUser;
} else {
// getUser returns promise
return this.data.getUser(params.user_id);
}
}
});
User controller:
var UserController = Em.ObjectController.extend({
needs: 'application',
isCurrent: function () {
return (this.get('id') === this.get('controllers.application.currentUser.id'));
}.property('id')
});
Profile route:
var uploaderScriptInjected = false;
var ProfileRoute = Em.Route.extend({
beforeModel: function () {
if (!uploaderScriptInjected && this.uploaderNeeded()) {
return $.getScript(this.config.assets.uploader)
.then(function () {
uploaderScriptInjected = true;
});
}
},
uploaderNeeded: function () {
// This is not working:
// return this.controllerFor('user').get('isCurrent');
},
model: function () {
var user = this.modelFor('user');
return Em.RSVP.hash({
photos: this.data.photosByUser(user.get('id'))
});
}
});
Take a look at beforeModel and uploaderNeeded methods of profile route. I want to use isCurrent property of user controller, but setupController method of user route is called after uploaderNeeded, which means that the instance of user controller hasn't been generated and the model for it hasn't been set. Of course I could use this code:
uploaderNeeded: function () {
return this.modelFor('application').get('currentUser.id') === this.modelFor('user').get('id');
}
...but it repeats isCurrent property logic of user controller.
Is there any way to access user controller's properties from profile route's methods?