1

如果这个问题太愚蠢,请原谅我。我自己正在研究 Ember.js,但我遇到了一个问题,我需要动态设置文本框的值,我现在不知道该怎么做我正在使用 jquery通过使用来克服这个错误$("#results")[0].value = data;,我知道这不是正确的方法我如何在 Ember,js 中做同样的事情

我的当前代码如下

App = Em.Application.create();
App.BaseCurrency = Em.Object.extend({
    id: null,
    name: null
});
App.DestCurrency = Em.Object.extend({
    id: null,
    name: null
});
App.BaseSelector = Em.ArrayController.extend({
    content: [
        App.BaseCurrency.create({ id: "INR", name:" Indian Rupee (INR)" }),
        App.BaseCurrency.create({ id: "USD", name: "US Dollar (USD)" })
    ]
});
App.baseSelector = App.BaseSelector.create();

App.DestSelector = Em.ArrayController.extend({
    content: [
        App.DestCurrency.create({ id: "INR", name: " Indian Rupee (INR)" }),
        App.DestCurrency.create({ id: "USD", name: "US Dollar (USD)" })
    ]
});
App.destSelector = App.DestSelector.create();

App.ConvertCurrency = Em.ObjectController.extend({
    content: [],
    amount:0,
    baseCur: null,
    destcur: null,
    result: 0,
    convert: function () {
        var amount = this.get('amount');
        var from = this.get('baseCur');
        var to = this.get('destCur');
        $.ajax({
            type: "POST",
            url: "Home/ConvertCurenncy",
            data: "{amount:" + amount + ",from:'" + from + "',to:'" + to + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $("#results")[0].value = data;
            }
        });
    }
});

App.convertCurrency = App.ConvertCurrency.create();

和 html

<script type="text/x-handlebars">
    AMOUNT : {{view Ember.TextField valueBinding="App.convertCurrency.amount"}}
    BASE : {{view Ember.Select contentBinding="App.baseSelector" optionLabelPath="content.name" optionValuePath="content.id" valueBinding="App.convertCurrency.baseCur" }}
    DEST : {{view Ember.Select contentBinding="App.destSelector" optionLabelPath="content.name" optionValuePath="content.id" valueBinding="App.convertCurrency.destCur" }}
    <button id="btnConvert"{{action "convert" target="App.convertCurrency"}}>convert</button>
    RESULT :<input type="text" readonly=true id="results" />
</script>
4

1 回答 1

2

您几乎在那里,您可能要做的是自定义 a Ember.Texfield,例如:

App.ResultField = Ember.TextField.extend({
  type: 'text',
  attributeBindings: ['readonly'],
  readonly: true
})

然后像这样使用它:

Result: {{view App.ResultField valueBinding="App.convertCurrency.result"}}

然后在您的success回调中设置App.convertCurrency.result为接收到的值:

...
success: function (data) {
  App.convertCurrency.set('result', data);
}
...

并且绑定将负责更新您的文本字段的值

希望能帮助到你。

于 2013-06-19T08:30:49.887 回答