I'm starting to play around with ember but one thing I haven't been able to wrap my head around is how to use one-way bindings, consider the following code:
HTML
<script type="text/x-handlebars">
<h1>Some App</h1>
<p>
My Name is: {{name}}
</p>
<form {{action 'updateName' on="submit"}}>
{{view Ember.TextField valueBinding="name"}}
<input type="submit" value="Save">
</form>
</script>
JS
var App = Ember.Application.create();
App.ApplicationRoute = Ember.Route.extend({
model: function() {
return {
name: 'John Doe'
}
},
events: {
updateName: function() {
console.log('Name Updated!');
}
}
});
By default the Ember.TextField
's value will be bound to my model and viceversa which means that when I type on the text the model and view will be updated live, however what I'm trying to do is bind the model to the textfield (so the initial name will be displayed) but update the model only when the form is submitted. Is there an easy way to do it?
Thanks in advance.
EDIT: Just for reference I updated my fiddle to use Ember.Binding.oneWay
I think the end result is cleaner than @c4p answer: http://jsfiddle.net/X2LmC/3/ however I'm not sure if doing $('#user-name').val()
to get the field value is the right way to do it.