我的link-to助手在Ember.js (最新的构建)中遇到了一个相当恼人的问题。不管我做什么,我都会在URL中得到undefined,这不仅看上去很愚蠢,而且使刷新页面变得不可能。我的模板中有以下内容:
{{#link-to 'organizations.organization.projects.index' this}}
Projects
{{/link-to}}this是对组织模型的引用。当我使用它时,Ember.js生成一个指向/organizations/undefined/organization.organization.projects的链接。然而,如果我点击链接,它仍然带我到页面与模型解决。当我将this替换为model时,也会发生同样的情况。
但是,如果我用this替换id,它生成的链接是正确的:/organizations/ORG_1/organization.organization.projects。但是当我点击链接时,URL不再显示ORG_1,而是显示undefined。但是它仍然能够正确地解决模型的问题。
id ORG_1模型是一种有效的模型。它不是未定义的,它不是空的,它是完全加载的。这个模型没有什么问题,但是Ember.js拒绝正确地将其ID放在URL中,而是设法在模板中使用它的数据。
有人知道是什么原因造成的吗?
这是我的路由器,供参考。(我知道,路线嵌套得很深。这个应用程序将非常大。)
App.Router.map(function() {
// route('application')
// route('index') -- main dashboard
this.route('settings');
this.resource('organizations', function() {
// route('index') -- organization selector page
this.resource('organizations.organization', { path: '/:organizationId' }, function() {
// route('index') -- organization dashboard
this.resource('organizations.organization.projects', function() {
// route('index') -- project selector page
this.resource('organizations.organization.projects.project', { path: '/:projectId' }, function() {
// route('index') -- project dashboard
});
});
});
});
// Must be the last route to ensure no others match.
this.route('missing', { path: '*path' });
});发布于 2014-01-03 16:28:05
this上没有属性organizationId,Ember从模型中获取该属性。
可以添加序列化钩子将organizationId映射到id。
App.OrganizationsOrganizationRoute = Em.Route.extend({
serialize:function(model){
return { organizationId: Em.get(model, 'id')};
}
})https://stackoverflow.com/questions/20907740
复制相似问题