gpt4 book ai didi

javascript - Ember.js 动态段不起作用

转载 作者:行者123 更新时间:2023-11-28 08:54:22 25 4
gpt4 key购买 nike

我正在使用 Ember 1.0.0 和最新版本的 Ember Data(测试版),并且我有一条包含无法正常工作的动态路段的路线。

我定义了以下路线:

PwdMgr.Router.map(function() {
this.resource("passwords", function(){
this.resource("password", {path: "/:password_id"}, function(){
this.route("edit");
});
});
}

在模板passwords.index中,我显示了这样的模型列表:

{{#each}}
<tr>
<td>{{id}}</td>
<td>{{name}}</td>
<td>{{client.name}}</td>
<td>{{service.name}}</td>
<td>{{localisation.name}}</td>
<td>{{status.name}}</td>
<td>{{login}}</td>
<td>{{password}}</td>
<td>
{{#link-to 'password.index' this}}<span class="glyphicon glyphicon-search"></span>{{/link-to}}
{{#link-to 'password.edit' this}}<span class="glyphicon glyphicon-pencil"></span>{{/link-to}}
<span class="glyphicon glyphicon-remove" {{action 'edit' password}}></span>
</td>
</tr>
{{/each}}

我有两个链接,一个链接到路由passwword.index,另一个链接到路由passwword.edit。我提供了动态段的模型,并且 Handlebars 正确创建了 URL(/passwords/1 和/passwords/1/edit)。

我的问题是,当我访问 URL/password/1(或/password/1/edit)时,模型不是单个对象,而是对象数组。

由于我使用的是默认模式(如指南中所述),因此我没有设置 Route 对象。但出于调试目的,我为password.index 路由创建了一个路由对象。它看起来像这样:

PwdMgr.PasswordIndexRoute = Ember.Route.extend({
model: function(params){
console.log(params);
return this.get('store').find('password',params.password_id);
},
setupController: function(controller, model){
console.log(model);
}

});

这是我的控制台日志:

Object {}                app.js (line 31)
<DS.RecordArray:ember435> { content=[3], store=<DS.Store:ember506>, isLoaded=true, more...} app.js (line 35)

空对象解释了为什么我得到一个对象数组,但是 params 变量是空对象是否有原因?

非常感谢

[编辑]

我已经改变了我的 Router.map,如下所示:

PwdMgr.Router.map(function() {
this.resource("passwords", function(){
this.route("detail", {path: "/:password_id"});
this.route("edit", {path: "/:password_id/edit"});
});
}):

“详细”和“编辑”路线的动态分段工作正常。我认为问题在于动态段位于嵌套资源中,这很奇怪,因为 Emberjs 指南的示例在嵌套资源中包含动态段。

最佳答案

password/1 似乎不是真正的路由,我会尝试passwords/1,并去掉路径上的斜杠。

PwdMgr.Router.map(function() {
this.resource("passwords", function(){
this.resource("password", {path: ":password_id"}, function(){
this.route("edit");
});
});
}

或者将其更改为

PwdMgr.Router.map(function() {
this.resource("passwords", function(){});
this.resource("password", {path: "password/:password_id"}, function(){
this.route("edit");
});
}

关于javascript - Ember.js 动态段不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18706005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com