问题描述
所以我最近在Em.TextField中找到了disabled属性,但是在将disabled属性设置为true后,似乎无法重新启用TextField。
So I've recently found the disabled attribute in the Em.TextField, however I can't seem to re-enable the TextField after I've extended it with the disabled property set to true.
var app = Em.Application.create();
app.randomObj = Em.Object.create({
temp: null
});
app.textField = Em.TextField.extend({
valueBinding: 'app.randomObj.temp',
disabled: true
});
如何使用Ember删除已禁用的属性?
How do I remove the disabled property with Ember?
推荐答案
您的示例代码有一些问题,我会处理每一个问题,希望我能清楚一些。
There are some issues with your sample code, I'll adress each one and hope I can clear some things up.
命名约定
首先你应该看看Emberist的帖子关于是应用程序.controller
和 App.ATextField
。
First of all you should have a look at the Emberist post about naming conventions: Classes should be named UpperCase and instances lowerCase - exception for applications and namespaces. So in your provided JSFiddle in your comment it's App.controller
and App.ATextField
.
将App声明为全局变量
您正在创建一个 Ember.Application
的实例,并将其分配给 var app
。虽然在使用全局变量时应该小心,但在这种情况下,您应该在全局命名空间上定义应用程序,因此应该是 App = Ember.Application.create();
分别为 window.App = Ember.Application.create();
。通过将应用程序声明为全局变量,您可以在模板和JS代码中使用强大的绑定功能。
You are creating an instance of an Ember.Application
and assigning it to var app
. Although you should be careful when using global variables, in this case you should define your application on the global namespace, so it should be App = Ember.Application.create();
respectively window.App = Ember.Application.create();
. By declaring the app as global variable, you can use the powerful Bindings feature in your templates and your JS code.
绑定
绑定应该在实例上声明,而不是在类上声明。这意味着你不会在你的类定义中定义 App.TextField
的 valueBinding
,而是移动这个例如在模板中。
The bindings should be declared on instances and not on classes. By this I mean you wouldn't define the valueBinding
of your App.TextField
in your class definition but rather move this to the concrete instance, for example in the template.
在您提供的JSFiddle中,您的您对控制器的绑定不起作用,因为您不使用它们:创建绑定到特定的控制器/对象/ ...你会必须声明要绑定到的属性名称并附加一个绑定
String。所以这将是 disabledBinding:'App.controller.shouldDisable'
。
In your provided JSFiddle in your comment your binding to the controller does not work because you don't use one: to create a binding to a specific controller/object/... you'll have to declare the property name you want to bind to and append a Binding
String. So it would be disabledBinding: 'App.controller.shouldDisable'
.
示例
我重构了代码,请参阅:
I've refactored your code, see http://jsfiddle.net/pangratz666/pLpKV/:
Handlebars :
{{view Ember.TextField
valueBinding="App.tempObj.hold"
disabledBinding="App.controller.shouldDisable"}} {{App.tempObj.hold}}
JavaScript :
App = Em.Application.create();
App.controller = Em.Object.create({
shouldDisable: true
});
App.tempObj = Em.Object.create({
hold: "initial value"
});
// just to illustrate how to define bindings outside of templates,
// we're adding a TextField with bindings setup the same as for the
// template
Ember.TextField.create({
valueBinding: 'App.tempObj.hold',
disabledBinding: 'App.controller.shouldDisable'
}).appendTo('body');
这篇关于Emberjs - 禁用并启用TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!