Skip to content

Commit 1174446

Browse files
Shuwen Qiananthonykoerber
authored andcommitted
Repeater api consistency with mm-form
1 parent 39c4e49 commit 1174446

3 files changed

Lines changed: 71 additions & 57 deletions

File tree

src/mm-repeater/index.html

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<html>
33
<head>
44
<script language="javascript" src="../../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
5-
<link rel="import" href="../../build/strand.html" />
6-
5+
<link rel="import" href="../strand.html" />
6+
77
<style type="text/css">
88
body, html {
99
height: 100%;
@@ -125,45 +125,52 @@
125125
</mm-repeater>
126126

127127
<mm-button style="margin: 10px 0;" id="validateBtn"><label>Validate!</label></mm-button>
128-
128+
129129
</div>
130130

131131
<script>
132+
var acceptedNames = ['John', 'Paul', 'George', 'Ringo'];
133+
var config = {
134+
'name': {
135+
validation: function(name) {
136+
return acceptedNames.indexOf(name) < 0;
137+
},
138+
errorMessage: 'Name is not in the list of accepted values'
139+
},
140+
'firstOption': {
141+
validation: function() {
142+
return true;
143+
}
144+
},
145+
'secondOption': {
146+
validation: function() {
147+
return true;
148+
}
149+
}
150+
};
151+
132152
var data = [
133153
{
134154
name: "John",
135155
firstOption: "Test Item 2",
136-
secondOption: "Test 1",
137-
validation: function(model) {
138-
console.log(arguments);
139-
// this.errorMessage = 'Setting error message in validation method';
140-
return model.name === 'John';
141-
},
142-
errorMessage: "Name is not John!"
156+
secondOption: "Test 1"
143157
},
144158
{
145159
name: "Paul",
146160
firstOption: "Test Item 1",
147-
secondOption: "Test 2",
148-
validation: function(model) {
149-
return model.name === 'Paul';
150-
},
151-
errorMessage: "Name is not Paul!"
161+
secondOption: "Test 2"
152162
},
153163
{
154164
name: "Ringo",
155165
firstOption: "Test Item 1",
156-
secondOption: "Test 3",
157-
validation: function(model) {
158-
return model.name === 'Ringo';
159-
},
160-
errorMessage: "Name is not Ringo!"
166+
secondOption: "Test 3"
161167
}
162168
];
163169

164170
window.addEventListener('WebComponentsReady', function(e) {
165171
var wd = document.querySelector('#withData');
166172
// wd.set('data', data);
173+
wd.config = config;
167174
wd.value = data;
168175

169176
document.querySelector('#validateBtn').addEventListener('click', function() {
@@ -173,8 +180,3 @@
173180
</script>
174181
</body>
175182
</html>
176-
177-
178-
179-
180-

src/mm-repeater/mm-repeater.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<link rel="import" href="../shared/behaviors/refable.html"/>
1010
<link rel="import" href="../shared/behaviors/resolvable.html"/>
1111
<link rel="import" href="../shared/behaviors/validatable.html"/>
12+
<link rel="import" href="../shared/js/validator.html"/>
13+
<link rel="import" href="../shared/js/datautils.html"/>
1214
<link rel="import" href="../mm-action/mm-action.html"/>
1315
<link rel="import" href="../mm-box/mm-box.html"/>
1416
<link rel="import" href="../mm-icon/mm-icon.html"/>

src/mm-repeater/mm-repeater.js

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@
66
*/
77
(function(scope) {
88

9+
var Validator = StrandLib.Validator,
10+
DataUtils = StrandLib.DataUtils;
11+
912
scope.Repeater = Polymer({
1013
is: 'mm-repeater',
1114

1215
properties: {
13-
template: {
16+
config: {
1417
type: Object,
15-
value: null
18+
value: {}
1619
},
17-
1820
data: {
1921
type: Array,
2022
notify: true,
2123
value: function() { return [{}]; },
2224
observer: '_handleDataChanged'
2325
},
2426

27+
template: {
28+
type: Object,
29+
value: null
30+
},
31+
2532
added: {
2633
type: Array,
2734
value: []
@@ -30,6 +37,7 @@
3037
type: Array,
3138
value: []
3239
},
40+
3341
_origData: {
3442
type: Array,
3543
value: []
@@ -127,39 +135,41 @@
127135
},
128136

129137
validate: function() {
130-
var allValid = true;
131-
132-
this.data.forEach(function(item, index) {
133-
var valid = true;
134-
135-
if(typeof item.validation === 'function') {
136-
// Custom validation provided: call validation, passing name:value pairs as arguments
137-
var elems = item._ref.querySelectorAll('[name]'),
138-
rowData = new Object();
139-
Object.keys(elems).forEach(
140-
function(key) {
141-
var elem = elems[key];
142-
rowData[elem.getAttribute('name')] = elem.value;
143-
}
144-
);
145-
valid = item.validation.call(item, rowData);
146-
} else {
147-
// Default validation: call validate on each form element and fold them together
148-
var fields = item._ref.querySelectorAll('[validation]');
149-
valid = Object.keys(fields).reduce(function(sum, elt) {
150-
return sum && (!fields[elt].validate || fields[elt].validate(fields[elt].value));
151-
}, true);
152-
}
138+
return this.data.reduce(function(sum, row) {
139+
var valid = this.validateRow(row);
140+
return sum && valid;
141+
}.bind(this), true);
142+
},
153143

154-
// Reflect validation to the model for error messaging
155-
this.set('data.'+index+'.error', !valid);
156-
this.set('data.'+index+'.errorMessage', item.errorMessage);
144+
validateRow: function(row) {
145+
var keys = Object.keys(row);
146+
var error = keys.reduce(function(sum, key) {
147+
var validation = DataUtils.getPathValue(key+'.validation', this.config),
148+
valid = true;
149+
150+
switch(typeof validation) {
151+
case "function":
152+
valid = validation.apply(this.config[key], [row[key], row, row._ref]);
153+
break;
154+
case "string":
155+
valid = Validator.rules[item](value);
156+
break;
157+
default:
158+
break;
159+
}
160+
return sum && valid;
157161

158-
allValid = allValid && valid;
162+
}.bind(this), true);
163+
var errorMessage = keys.reduce(function(sum, key) {
164+
var message = DataUtils.getPathValue(key+'.errorMessage', this.config);
165+
return sum + " " + (message || "");
166+
}.bind(this), "");
159167

160-
}, this);
168+
var prefix = 'data.' + this.data.indexOf(row) + '.';
169+
this.set(prefix+'error', error);
170+
this.set(prefix+'errorMessage', errorMessage.trim());
161171

162-
return allValid;
172+
return error;
163173
},
164174

165175
validation: this.validate,

0 commit comments

Comments
 (0)