Javascript Ember.js:访问父路由';s控制器
我的应用程序中的用户有个人资料页面。用户可以查看自己的个人资料和其他用户的个人资料。在他自己的个人资料中,他可以更改他的头像,但当然他不能更改其他用户的头像。使用配置文件路径的beforeModelhook,根据需要加载用于上传化身的脚本。问题是,仅当用户访问自己的配置文件而不是其他用户的配置文件时,才加载此脚本 这是我的密码: 路由器:Javascript Ember.js:访问父路由';s控制器,javascript,ember.js,Javascript,Ember.js,我的应用程序中的用户有个人资料页面。用户可以查看自己的个人资料和其他用户的个人资料。在他自己的个人资料中,他可以更改他的头像,但当然他不能更改其他用户的头像。使用配置文件路径的beforeModelhook,根据需要加载用于上传化身的脚本。问题是,仅当用户访问自己的配置文件而不是其他用户的配置文件时,才加载此脚本 这是我的密码: 路由器: Router.map(function () { this.resource('user', { path: '/:user_id' }, func
Router.map(function () {
this.resource('user', { path: '/:user_id' }, function () {
this.resource('profile');
});
});
var ApplicationRoute = Em.Route.extend({
model: function () {
return {
currentUser: this.config.currentUser
};
}
});
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);
}
}
});
var UserController = Em.ObjectController.extend({
needs: 'application',
isCurrent: function () {
return (this.get('id') === this.get('controllers.application.currentUser.id'));
}.property('id')
});
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'))
});
}
});
申请途径:
Router.map(function () {
this.resource('user', { path: '/:user_id' }, function () {
this.resource('profile');
});
});
var ApplicationRoute = Em.Route.extend({
model: function () {
return {
currentUser: this.config.currentUser
};
}
});
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);
}
}
});
var UserController = Em.ObjectController.extend({
needs: 'application',
isCurrent: function () {
return (this.get('id') === this.get('controllers.application.currentUser.id'));
}.property('id')
});
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'))
});
}
});
用户路线:
Router.map(function () {
this.resource('user', { path: '/:user_id' }, function () {
this.resource('profile');
});
});
var ApplicationRoute = Em.Route.extend({
model: function () {
return {
currentUser: this.config.currentUser
};
}
});
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);
}
}
});
var UserController = Em.ObjectController.extend({
needs: 'application',
isCurrent: function () {
return (this.get('id') === this.get('controllers.application.currentUser.id'));
}.property('id')
});
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'))
});
}
});
用户控制器:
Router.map(function () {
this.resource('user', { path: '/:user_id' }, function () {
this.resource('profile');
});
});
var ApplicationRoute = Em.Route.extend({
model: function () {
return {
currentUser: this.config.currentUser
};
}
});
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);
}
}
});
var UserController = Em.ObjectController.extend({
needs: 'application',
isCurrent: function () {
return (this.get('id') === this.get('controllers.application.currentUser.id'));
}.property('id')
});
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'))
});
}
});
配置文件路径:
Router.map(function () {
this.resource('user', { path: '/:user_id' }, function () {
this.resource('profile');
});
});
var ApplicationRoute = Em.Route.extend({
model: function () {
return {
currentUser: this.config.currentUser
};
}
});
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);
}
}
});
var UserController = Em.ObjectController.extend({
needs: 'application',
isCurrent: function () {
return (this.get('id') === this.get('controllers.application.currentUser.id'));
}.property('id')
});
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'))
});
}
});
查看配置文件路由的beforeModel
和uploadereded
方法。我想使用user controller的isCurrent
属性,但是setupController
方法在uploadereded
之后被调用,这意味着还没有生成user controller的实例,也没有设置它的模型。当然,我可以使用以下代码:
uploaderNeeded: function () {
return this.modelFor('application').get('currentUser.id') === this.modelFor('user').get('id');
}
…但它重复用户控制器的isCurrent
属性逻辑
是否有任何方法可以从配置文件路由的方法访问用户控制器的属性?例如,尝试扩展父控制器
App.FirstController=Ember.ArrayController.extend({})
然后
App.SecondController=Ember.FirstController.extend({})
也许我遗漏了一些东西,但它如何帮助我呢?你可以制作扩展UserController的概要文件控制器。。这一定行得通,但我不确定