0

I am working on a simple Ember CRUD app and ran into an issue swapping a "display" template with an "edit" template. Here is my route configuration:

App.Router.map(function(){ //map URLs to templates
   this.resource('contacts',{path: '/contacts'}, function(){
       this.resource('contact', {path: '/contact/:contact_id'}, function(){
           this.route('edit');
           this.route('create');
           this.route('delete');
       });
   });
});

The follow template displays my model. I want the link-to to replace the display template with the edit template:

<script type="text/x-handlebars" data-template-name="contact">
        <h3>{{ firstName }} {{ lastName }}</h3>
        <h4>Contact Details</h4>
            {{ email }} 
        <br/>
        {{ phone }}
        <br/>
        {{#link-to "contact.edit" this}}edit{{/link-to}}
</script> 

Unfortunately when a user clicks the #link-to "contacdt.edit", the view is rendered in the {{outlet}} (I only added the {{outlet}} for debugging). The edit template also doesn't seem to bind correctly to the current model.

Please see this jsfiddle for a complete example.

4

1 回答 1

4

默认情况下,模板在父模板的主出口中呈现。由于您的路线层次结构,您的contact.edit模板具有contact作为父模板。您需要在contacts模板内部进行渲染。

您可以覆盖renderTemplate方法 from App.ContactEditRoute,以获得所需的行为:

App.ContactEditRoute = Em.Route.extend({
    model: function (params) {
        return this.store.find(App.Contact, params.contact_id);
    },
    actions: {
        save: function () {
            var newContact = this.modelFor('contact.edit');
            this.transitionTo('contact', newContact);
        }
    },
    renderTemplate: function() {
        this.render('contact.edit', { into: 'contacts' })
    }
});

这是模板渲染的文档http://emberjs.com/guides/routing/rendering-a-template/

这是更新代码的小提琴http://jsfiddle.net/marciojunior/y58vB/

于 2013-10-25T22:35:09.700 回答