我有一个输入字段的日期验证器:
export default Ember.Component.extend({
moment: Ember.inject.service(),
dateValidation: [{
message: 'invalid date',
validate: function(inputDate) {
if (!inputDate) {
return true; //allow empty date
}
return this.get('moment').moment(inputDate, 'YYYY-MM-DD', true).isValid();
}
}],
});我使用ember-moment来验证日期。我通常使用this.get('moment').moment(...),但在函数中没有定义this.get() (没有定义get()函数,因为this引用了验证器函数,而不是具有get()函数的ember组件)。我知道this引用的是函数,而不是我的组件,但是我如何修复它呢?使用Ember.get()对我不起作用。
我使用的是ember-paper,并且paper-input有一个customValidations-attribute,它必须是一个数组(在我的例子中是dateValidation)
例如:
{{paper-input
type="date"
value=atDate
onChange=(action (mut atDate))
customValidations=dateValidation}}发布于 2016-11-07 16:29:00
this引用传递给验证方法。moment。导出默认消息({ moment: Ember.inject.service(),dateValidation:[],init() { this._super(...arguments);var objectValidation ={ message:'invalid date',myMoment: this.get('moment'),validate: function(inputDate) { if (!inputDate) { return true;//允许空日期} return this.get('myMoment').moment(inputDate,'YYYY-MM-DD',true).isValid();} };true}
});
发布于 2016-11-09 01:48:02
我无法对此进行测试,但一个简单的bind看起来应该可以解决这个问题:
export default Ember.Component.extend({
moment: Ember.inject.service(),
dateValidation: [{
message: 'invalid date',
validate: function(inputDate) {
if (!inputDate) {
return true; //allow empty date
}
return this.get('moment').moment(inputDate, 'YYYY-MM-DD', true).isValid();
}.bind(this)
}],
});https://stackoverflow.com/questions/40460330
复制相似问题