简体   繁体   中英

Ember.js Dynamic segment not working

I'm using Ember 1.0.0 and the latest build of Ember Data (beta), and I have a route with a dynamic segment that isn't working.

I have defined the routes below:

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

In the template passwords.index I display a list of models like this:

{{#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}}

I have two links, one that goes to the route password.index and one to the route passwword.edit. I provide the model for the dynamic segment and the handlebars creates the URL correctly (/passwords/1 and /passwords/1/edit).

My problem is that when I get to the URL /password/1 (or /password/1/edit), the model is not a single object but an array of objects.

Since I'm using the default pattern, as explained in the guides, I didn't setup Route objects. But for debugging purposes I created a route object for the password.index route. Here's what it looks like:

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

});

And here's my console log:

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

The empty object explains why I get an array of object but is there a reason why the params variable is an empty object?

Thanks a lot

[EDIT]

I have changed my Router.map like so:

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

And the dynamic segment for both "detail" and "edit" routes works fine. I think the problem comes from the fact that the dynamic segment is in the nested resource, which is strange because the Emberjs guides' examples are with dynamic segments in nested resources.

password/1 doesn't appear to be a real route, I'd try passwords/1, and get rid of the slash on the path.

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

Or change it to

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

I found my mistake thanks to Daniel's comment.

My routes were setup like this (before my edit):

passwords
 password
  password.detail
  password.edit
 password.index

I was using the route PwdMgr.PasswordsRoute to setup my models and its corresponding template passwords.

The problem was that I was in the passwords route and going directly to the password.detail route. I think there was a problem going from passwords to password.detail (or password.edit) with the model parameter.

Anyway, once I changed my route to PwdMgr.PasswordsIndexRoute and the corresponding template to passwords/index, everything was going as expected. Models were passed correctly through the dynamic segment.

Thanks a lot Daniel for pointing out my error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM