简体   繁体   中英

Ember.js: Relationship between view input and model

I'm brand new to Ember.js, but well-versed in MVC and Backbone (even cocoa and smalltalk), but for some reason, Ember's insistence on its highly-obscured and inconsistent API is shielding me from understanding 1) binding and 2) how that relates to persisting records.

I have a view that has an input. I know that. I have a model that belongs to a controller. I know that. I don't understand how the view knows (or is supposed to know) about the controller, and I don't understand how the model gets the text I type into the view when I'm ready to push it to the backend (ostensibly with .save() ). These are supposed to be boilerplate style things that Ember breathlessly abstracts away, but I find I'm having to go through the Ember.js source just to understand what the heck is happening.

I've read the docs, and their examples are so contrived as to be unusable (featuring only the most basic examples, which is pointless when we're talking about "ambitious web apps", which ember purports to be for).

It should be noted that we are using ember-data in house, and this particular route has multiple views, controllers and models.

So let's say you have an input in the index template:

<script type="text/x-handlebars">
     {{input type="text" value=name}}
</script>

Ember automatically binds the App.IndexController to this template and the name property of the controller to the value property of the input field. As the value is changed in the input, it'll automatically update in the controller. When you're ready to save it in the model you can access the property in a action.

App.IndexController = Ember.ObjectController.extend({
    actions: {
        save:function() {
            var name = this.get('name');
            var model = this.store.createRecord('item');
            item.set('name',name);
            item.save();
        }
    }
});

This will trigger a post request to /items to store the item.

If you have a more specific example in mind, I can try to show you how that works.

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