【问题标题】:Ember.js Dynamic segment not workingEmber.js 动态段不起作用
【发布时间】:2013-09-13 09:37:13
【问题描述】:

我正在使用 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}}

我有两个链接,一个指向路径 password.index,一个指向路径 passwword.edit。我提供了动态段的模型,并且车把正确地创建了 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 指南的示例是嵌套资源中的动态段。

【问题讨论】:

    标签: javascript ember.js ember-data handlebars.js


    【解决方案1】:

    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");
       });
     }
    

    【讨论】:

    • 我已经尝试过该解决方案,但我仍然遇到同样的问题。似乎嵌套资源“密码”导致了问题(参见问题编辑)。
    • 你编辑的版本肯定不标准。资源是一组共享相同资源/模型的路由。 emberjs.com/guides/routing/defining-your-routes
    【解决方案2】:

    感谢丹尼尔的评论,我发现了我的错误。

    我的路线是这样设置的(在我编辑之前):

    passwords
     password
      password.detail
      password.edit
     password.index
    

    我使用 PwdMgr.PasswordsRoute 路由来设置我的模型及其相应的模板密码。

    问题是我在 passwords 路由中并直接进入 password.detail 路由。我认为使用模型参数从密码到 password.detail(或 password.edit)存在问题。

    无论如何,一旦我将路由更改为 PwdMgr.PasswordsIndexRoute 并将相应的模板更改为密码/索引,一切都按预期进行。模型已正确通过动态段。

    非常感谢 Daniel 指出我的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      相关资源
      最近更新 更多