简体   繁体   中英

Ember.js get attributes from template using {{render}} helper

This question is related to this one: Ember.js {{render}} helper model not correctly set But I think that I ask the wrong question.

Router

App.Router.map(function () {
    this.resource('article', {path: '/article/:id'});
    this.resource('article.new', {path: "/article/new"});
});

I have not defined a route or resource for categorynew because it is rendered as a popup within both Article and Article.new.

Template

<script type="text/x-handlebars" data-template-name="article">
    {{render "category/new"}}
</script>

<!-- popups -->
<script type="text/x-handlebars" data-template-name="category/new">
        Name: {{input type="text" value=name}}
        Image: {{view App.UploadFile name="image" file=image }}
        Category-parent: {{input value=categoryRelation}}

        <button {{action 'saveCategory'}}>Save</button>
</script>

Controller

App.CategoryNewController = Ember.ObjectController.extend({
    actions: {
        saveCategory: function () {
            var newCategory = this.store.createRecord('category', {
                name: this.get('name'),
                image: this.get('image'),
                category_parent:this.get('category_parent')
            });

            newCategory.save();
            console.log(this.get('naam')); // undefinded
        }
    }
});

When I fill the form that gets rendered with {{render category/new}} I get these errors:

Assertion failed: Cannot delegate set('name', a) to the 'content' property of object proxy <App.CategoryNewController:ember387>: its 'content' is undefined. ember-1.1.2.js:417

Uncaught Error: Object in path nam could not be found or was destroyed.

I think there must be a model in the controller. But if I do a this.get('model') it is always the wrong model. Even if I define it in App.CategoryNewRoute .

When you call render you can supply it a model, but you aren't supplying it a model. Your controller on the other hand extends ObjectController, which tells ember it's backed by a model. So either you can supply it a model, or you can change it to extend Controller (and everything will live on the controller instead of on a non-existent model).

App.CategoryNewController = Ember.Controller.extend({

name is spelled wrong in the console.log, but I'm pretty sure that's just a typo while putting it on SO.

http://emberjs.jsbin.com/EtafEFUr/1/edit

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