Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from PolymerElements/fix-meta-bonkerness
Browse files Browse the repository at this point in the history
use a global IronMeta instance
  • Loading branch information
notwaldorf committed Apr 26, 2016
2 parents 93e8d8f + 8759f52 commit fdcb23a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
44 changes: 29 additions & 15 deletions iron-validatable-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<link rel="import" href="../iron-meta/iron-meta.html">

<script>
/**
* Singleton IronMeta instance.
*/
Polymer.IronValidatableBehaviorMeta = null;

/**
* `Use Polymer.IronValidatableBehavior` to implement an element that validates user input.
Expand Down Expand Up @@ -41,14 +45,6 @@

properties: {

/**
* Namespace for this validator.
*/
validatorType: {
type: String,
value: 'validator'
},

/**
* Name of the validator to use.
*/
Expand All @@ -66,22 +62,35 @@
value: false
},

/**
* This property is deprecated and should not be used. Use the global
* validator meta singleton, `Polymer.IronValidatableBehaviorMeta` instead.
*/
_validatorMeta: {
type: Object
}
},

/**
* Namespace for this validator. This property is deprecated and should
* not be used. For all intents and purposes, please consider it a
* read-only, config-time property.
*/
validatorType: {
type: String,
value: 'validator'
},

_validator: {
computed: '__computeValidator(validator)'
}
},

observers: [
'_invalidChanged(invalid)'
],

get _validator() {
return this._validatorMeta && this._validatorMeta.byKey(this.validator);
},

ready: function() {
this._validatorMeta = new Polymer.IronMeta({type: this.validatorType});
registered: function() {
Polymer.IronValidatableBehaviorMeta = new Polymer.IronMeta({type: 'validator'});
},

_invalidChanged: function() {
Expand Down Expand Up @@ -128,6 +137,11 @@
return this._validator.validate(value);
}
return true;
},

__computeValidator: function() {
return Polymer.IronValidatableBehaviorMeta &&
Polymer.IronValidatableBehaviorMeta.byKey(this.validator);
}
};

Expand Down
30 changes: 30 additions & 0 deletions test/cats-only.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="../../iron-validator-behavior/iron-validator-behavior.html">

<script>

Polymer({

is: 'cats-only',

behaviors: [
Polymer.IronValidatorBehavior
],

validate: function(value) {
return value === 'cats';
}

});

</script>
30 changes: 30 additions & 0 deletions test/dogs-only.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="../../iron-validator-behavior/iron-validator-behavior.html">

<script>

Polymer({

is: 'dogs-only',

behaviors: [
Polymer.IronValidatorBehavior
],

validate: function(value) {
return value === 'dogs';
}

});

</script>
29 changes: 29 additions & 0 deletions test/iron-validatable-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="test-validatable.html">
<link rel="import" href="cats-only.html">
<link rel="import" href="dogs-only.html">

</head>
<body>
Expand All @@ -34,6 +36,14 @@
</template>
</test-fixture>

<test-fixture id="validators">
<template>
<cats-only></cats-only>
<dogs-only></dogs-only>
<test-validatable></test-validatable>
</template>
</test-fixture>

<script>

suite('basic', function() {
Expand All @@ -52,6 +62,25 @@
assert.isTrue(valid);
});

test('changing the validator works', function() {
var node = fixture('validators');
var input = node[2];

// Initially there's no validator, so everything is valid.
assert.isTrue(input.validate(''));
assert.isTrue(input.validate('cats'));

// Only valid if the value is 'cats'.
input.validator = 'cats-only';
assert.isFalse(input.validate('ca'));
assert.isTrue(input.validate('cats'));

// Only valid if the value is 'dogs'.
input.validator = 'dogs-only';
assert.isFalse(input.validate('cats'));
assert.isTrue(input.validate('dogs'));
});

});

</script>
Expand Down

0 comments on commit fdcb23a

Please sign in to comment.