这就是问题所在。我目前正在创建一个混合器来处理表单验证。问题是,我想在init方法中创建一个计算属性isFormValid,它是'computed.and'属性的'is<fieldName>Valid'其他属性。
我可以创建它,但它永远不会更新。我想我需要增加观察员,但也许有人会有一个更好的解决方案?
编辑
这里有一些澄清。
我的控制器得到了这个属性:
App.FormViewController = Ember.Controller.extend(App.ValidatorMixin, {
validations: {
field1: {
errLvl: App.Validation.ErrLvl.ERROR,
type: App.Validation.Type.TEXT,
pattern: /^\d{6}$/,
message: 'Error message'
},
field2: {
//somecode
}
}
});混合器的定义如下:
App.ValidatorMixin = Ember.Mixin.create({
init: function() {
this._super();
var self = this;
Ember.keys(this.validations).forEach(function(prop) {
self.set('is' + prop.capitalize() + 'Valid', false); //Is changed when the field is valid
});
}
});isFormValid属性应该是所有这些is<fieldName>Valid的Ember.computed.and。
EDIT2
表单上的每个输入都是由输入助手和这个混合器定义的:
App.Mixin.ValidatableInput = Ember.Mixin.create({
focusOut: function() {
this.validate();
},
//Do the validation
validate: function() {
//I'm currently moving that part to the controller because it's part
//of the logic but it was easier for a start to write it here
//We update the is<fieldName>Valid property
this.get('parentView.controller').set('is' + this.get('name').capitalize() + 'Valid', !hasError);
//Then some DOM manipulation to attach the error message
}
});最后我的观点是这样的
{{view App.CustomTextField name="field1" value=field1}}
{{view view.buttonCreate disabled=isFormInvalid}}发布于 2014-04-29 10:04:17
在init方法中,可以使用defineProperty设置计算属性。
Ember.defineProperty(myObject, 'myProperty', Ember.computed(function computed() {
return 'myValue';
}).property('myObserver'));发布于 2014-04-29 15:43:18
这对你有用吗?(扩展@Wild蜂蜜,我不能评论)。如果有以下几个属性,则可以指定多个属性:
Ember.defineProperty(myObject, 'myProperty', Ember.computed(function computed() {
return 'myValue';
}).property('myObserver.@each.myObservedProperty'));我有幸在Ember中使用jQuery验证,触发表单视图在didInsertElement上的.validate()调用和规则,然后当您将焦点放在每个Ember.TextField上时,进行.valid()检查。我强烈推荐它用于表单验证。
https://stackoverflow.com/questions/23361010
复制相似问题