emberjs绑定

时间:2012-07-02 21:28:22

标签: 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
    }
});
<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>

1 个答案:

答案 0 :(得分:10)

您需要使用set之类的

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

而不是你在做什么。

this.name = '';

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

Here is a working fiddle.

相关问题