【问题标题】:How to extend TextField to access model in Ember.js如何扩展 TextField 以访问 Ember.js 中的模型
【发布时间】:2014-04-22 03:57:29
【问题描述】:

我正在 ember.js 中写一个表单帖子。

  • attr 绑定和条件仅支持除表达式以外的布尔值。
  • 动作没有参数。

我希望仅在输入获得焦点且输入有效时显示错误。

简单来说,我应该在 focus-in 和 focus-out 属性中做到这一点。但是有这么多字段,我必须为每个输入编写两个动作。

我想写一个可重用的视图来做到这一点,但我不知道如何改变模型在视图中的值。

有人知道吗?

【问题讨论】:

    标签: ember.js


    【解决方案1】:
    Erp.FromFieldView = Ember.TextField.extend({
    
        attributeBindings: ['type', 'value', 'size', 'pattern', 'name', 'min', 'max',
                          'accept', 'autocomplete', 'autosave', 'formaction',
                          'formenctype', 'formmethod', 'formnovalidate', 'formtarget',
                          'height', 'inputmode', 'list', 'multiple', 'pattern', 'step',
                          'width', 'error', 'regex'],
        name: 'default',
    
        error: '',
    
        regex: '^.+$',
    
        isValid: true,
    
    
        valueChanged: function () {
            re = new RegExp(this.get('regex'), 'g');
            this.set('error', !re.test(this.get('value')));
        }.observes('value'),
    
        focusIn: function (event) {
            this._super(event);
            if (this.get('value') == undefined) {
                this.set('error', true);
            }
        },
    
        focusOut: function (event) {
            this._super(event);
            this.set('error', false);
        }
    });
    
    Ember.Handlebars.helper('form-field', Erp.FromFieldView);
    

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 1970-01-01
      • 2014-10-13
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多