Ember.js:具有动态绑定的 TextField [英] Ember.js: TextField with dynamic binding
问题描述
我想将 TextField 绑定到由字符串变量指定的属性(请参阅编辑以获得更好的解释),如 这个问题.不幸的是,那里给出的答案不再有效.他们在那里使用以下视图:
I want to bind a TextField to a property which is specified by a string variable (see the edit for a better explanation), as in this question. Unfortunately the answer that was given there does not work anymore. They use the following view there:
App.AutoTextField = Ember.ContainerView.extend({
type: null,
name: null,
init: function() {
this._super();
this.createChildView();
},
createChildView: function() {
this.set('currentView', Ember.TextField.create({
valueBinding: 'controller.' + this.get('name'),
type: this.get('type')
}));
}.observes('name', 'type')
});
到目前为止我能得到的最好的方法是用路径 _parentView.context.
替换 valueBinding
.如果我这样做,文本字段将被呈现并且它们包含正确的值.如果我编辑它们,应用程序的其余部分不会得到更新.
The best I could get until now was replacing the valueBinding
with the path _parentView.context.
. If I do this the text fields get rendered and they contain the correct value. If I edit them the rest of the application doesn't get updated though.
您将如何在当前版本的 Ember 中解决此问题?
How would you solve this in the current version of Ember?
在链接的问题中给出了我想要做的更好的解释.我在当前上下文中有一个对象(比如 object
)和一个字符串(key
).我想要一个可以显示和呈现 object[key]
值的文本字段.
A better explanation of what I want to do is given in the linked question. I have an object (say object
) and a string (key
) in the current context. I want a text field that can show and render the value of object[key]
.
推荐答案
经过多次尝试失败后,我找到了一个非常简单的解决方案.
After many failed attempts I found a pretty simple solution.
添加助手
Ember.Handlebars.helper('dataTextField', function (data, key, options) {
options.hash.valueBinding = 'data.' + key;
return Ember.Handlebars.helpers.input.apply(this, [options]);
});
然后调用
{{dataTextField data key}}
这将呈现一个文本字段,显示和更改 data[key]
的值,它甚至支持普通 input
助手可以理解的所有可选参数.
This will render a text field that shows and changes the value of data[key]
and it even supports all the optional arguments the normal input
helper can understand.
这篇关于Ember.js:具有动态绑定的 TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!