I am having trouble with binding on emberjs. I have bound an ember textfield to a variable inside a controller. When I write into the text field the bound variable gets updated correctly.
Now I want to change the variable (and the text in the text field) through JS. Nothing happens when I do.
App = Ember.Application.create({});
App.FormInfo = Em.TextField.extend({
insertNewline: function(){
App.AController.clear();
}
});
App.AController = Em.ArrayController.create({
content: [],
name: '',
clear: function(){ //I want this function to clear the text field and set name to an empty string
this.name = '';
console.log(this.name);//expected empty string; actual user input
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.5.min.js"></script>
<script type="text/x-handlebars">
{{view App.FormInfo placeholder="Name" valueBinding="App.AController.name"}}
</script>
You need to use set
like so
this.set('name', '');
instead of what you were doing.
this.name = '';
The KVO/Binding stuff only happens when you use the compliant methods; this is why those methods exist in the first place.
Here is a working fiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With