问题
I'm trying to get some data out my templates to, let's say, add an user.
This is what my view object looks like:
Ember.View.create({
template: Ember.Handlebars.compile( UsersIndexTemplate ),
addUser: function() {
var value = $('#my-awesome-input').val();
}
})
In theory I got my value already with jQuery, but is this the preferred way to go?
回答1:
You should use bindings to bind the values in your template to properties on your view, see an example http://jsfiddle.net/pangratz666/uuvAp/:
Handlebars:
<script type="text/x-handlebars" data-template-name="my-template" >
<!-- bind value of TextField to property username of view -->
Username: {{view Ember.TextField valueBinding="view.username"}}
<button {{action addUser}}>addUser</button>
</script>
JavaScript:
Ember.View.create({
templateName: 'my-template',
addUser: function(event){
var username = this.get('username');
console.log('add user with name: %@'.fmt(username));
}
}).append();
回答2:
You can use the available insertNewline for handling the value.
See this fiddle http://jsfiddle.net/z5SNW/6/
来源:https://stackoverflow.com/questions/11138389/preferred-way-to-retrieve-data-from-a-template-in-emberjs