emberjs绑定

Nin*_*ino 5 ember.js

我在使用emberjs时遇到麻烦.我已将ember textfield绑定到控制器内的变量.当我写入文本字段时,绑定变量会正确更新.

现在我想通过JS更改变量(以及文本字段中的文本).我什么都没发生.

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
    }
});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

hvg*_*des 11

你需要使用set像这样

this.set('name', '');

而不是你在做什么.

this.name = '';

只有在使用兼容方法时才会发生KVO /绑定事件; 这就是为什么这些方法首先存在的原因.

这是一个工作小提琴.