Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js ember cp验证未在每次更改依赖密钥时执行_Ember.js_Ember Cp Validations - Fatal编程技术网

Ember.js ember cp验证未在每次更改依赖密钥时执行

Ember.js ember cp验证未在每次更改依赖密钥时执行,ember.js,ember-cp-validations,Ember.js,Ember Cp Validations,让我分享一下我很久以前偶然发现并在今天解决的问题 问题描述: 验证程序不会在每次依赖密钥更改时执行 我的自定义验证器检查自定义元键的唯一性定义如下: import BaseValidator from 'ember-cp-validations/validators/base'; import Ember from 'ember'; const {isEqual} = Ember; export default BaseValidator.extend({ /** * Valida

让我分享一下我很久以前偶然发现并在今天解决的问题

问题描述: 验证程序不会在每次依赖密钥更改时执行

我的自定义验证器检查自定义元键的唯一性定义如下:

import BaseValidator from 'ember-cp-validations/validators/base';
import Ember from 'ember';
const {isEqual} = Ember;

export default BaseValidator.extend({

  /**
   * Validates custom-metas of the {spot} model.
   * The validation leans upon duplicates detection of the 'key' property values.
   * @example:
   * spot.set('customMeta', [{key: 'duplicate'}, {key: 'duplicate'}]);
   * spot.get('validations.attrs.customMeta.isValid') -> false
   * spot.set('customMeta', [{key: 'unique 1'}, {key: 'unique 2'}]);
   * spot.get('validations.attrs.customMeta.isValid') -> true
   * ...skipping rest of the doc...
   */
  validate(value, options, spot) {

    const customMetaKeys = spot.get('customMeta').mapBy('key');

    if(isEqual(customMetaKeys.get('length'), customMetaKeys.uniq().get('length'))){
      return true;
    }

    return this.createErrorMessage('unique-custom-meta-keys', value, options);
  }

});
验证程序被执行了两次,尽管依赖键的更改频率更高。我认为问题可能来自加载项或观察器,它们是在与其他特性相关的相同条件下启动的

这是我的验证声明:

const Validations = buildValidations({
  customMeta: {
    description: 'Custom-metas',
    validators: [
      validator('unique-custom-meta-key', {
        dependentKeys: ['customMeta.@each.key'],
        debounce: 500
      })
    ]
  }
});
和模型定义:

export default Model.extend(Validations, {
  customMeta : fragmentArray('custom-meta')
});
解决方案: 在查看代码之后,我注意到声明一个依赖于集合中多个值的验证器的区别:

dependentKeys: ['model.friends.@each.name']
如您所见,dependent keys声明中的
model
属性起到了作用。如今,他们也提供了一个正确的声明,而这在我第一次偶然发现这个问题时是不正确的

dependentKeys: ['model.customMeta.@each.key'],
非常愚蠢的错误,但也许这条线可以挽救某人的一天;-)