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')
});
我可以得到的最好的代码是将$ code> valueBinding 路径 _parentView.context。
。如果我这样做,文本字段被渲染,它们包含正确的值。如果我编辑它们,其余的应用程序不会被更新。
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.
添加帮助者
Add the helper
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]
的值,甚至支持普通输入
帮助器可以理解的所有可选参数。
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屋!