Ember model is gone when I use the renderTemplate hook

Posted by Mickael Caruso on Stack Overflow See other posts from Stack Overflow or by Mickael Caruso
Published on 2014-06-03T15:23:25Z Indexed on 2014/06/03 15:24 UTC
Read the original article Hit count: 277

Filed under:
|

I have a single template - editPerson.hbs

<form role="form">
    FirstName: {{input type="text" value=model.firstName }} <br/>
    LastName: {{input type="text" value=model.lastName }}
</form>

I want to render this template when the user wants to edit an existing person or create a new person. So, I set up routes:

App.Router.map(function(){
    this.route("createPerson", { path: "/person/new" });
    this.route("editPerson", { path: "/person/:id});
    // other routes not show for brevity
});

So, I define two routes - one for create and one for edit:

App.CreatePersonRoute = Ember.Route.extend({
     renderTemplate: function(){
          this.render("editPerson", { controller: "editPerson" });
     },
     model: function(){
          return {firstName: "John", lastName: "Smith" };
     }
});

App.EditPersonRoute = Ember.Route.extend({     
     model: function(id){
          return {firstName: "John Existing", lastName: "Smith Existing" };
     }
});

So, I hard-code the models. I'm concerned about the createPerson route. I'm telling it to render the editPersonTemplate and to use the editPerson controller (which I don't show because I don't think it matters - but I made one, though.)

When I use renderTemplate, I lose the model John Smith, which in turn, won't display on the editTemplate on the web page. Why?

I "fixed" this by creating a separate and identical (to editPerson.hbs) createPerson.hbs, and removing the renderTemplate hook in the CreatePerson. It works as expected, but I find it somewhat troubling to have a separate and identical template for the edit and create cases.

I looked everywhere for how to properly do this, and I found no answers.

© Stack Overflow or respective owner

Related posts about ember.js

Related posts about yeoman