Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emberjs binding

Tags:

ember.js

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>
like image 913
Nino Avatar asked Jul 02 '12 21:07

Nino


1 Answers

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.

like image 143
hvgotcodes Avatar answered Oct 03 '22 18:10

hvgotcodes