|
14 | 14 | type: Object, |
15 | 15 | value: null |
16 | 16 | }, |
| 17 | + |
17 | 18 | data: { |
18 | 19 | type: Array, |
19 | 20 | notify: true, |
20 | | - value: function() { return []; }, |
21 | | - observer: '_handleDataChanged' |
| 21 | + value: function() { return [{}]; } |
22 | 22 | }, |
| 23 | + |
23 | 24 | addRowLabel: { |
24 | 25 | type: String, |
25 | 26 | value: '+Add Item' |
|
32 | 33 | StrandTraits.Validatable |
33 | 34 | ], |
34 | 35 |
|
35 | | - get value() { return this.data; }, |
36 | | - set value(newVal) { if (newVal instanceof Array) this.data = newVal; }, |
| 36 | + get value() { |
| 37 | + return this.data; |
| 38 | + }, |
| 39 | + |
| 40 | + set value(newVal) { |
| 41 | + if (newVal instanceof Array) { |
| 42 | + this.set('data', newVal); |
| 43 | + } |
| 44 | + }, |
| 45 | + |
| 46 | + observers: [ |
| 47 | + '_injectModelData(data.*)' |
| 48 | + ], |
| 49 | + |
| 50 | + _injectModelData: function(e) { |
| 51 | + var path = e.path.split('.'), |
| 52 | + record = path[path.length-1]; |
| 53 | + |
| 54 | + console.log(e); |
| 55 | + if(record === '_ref') { |
| 56 | + var index = parseInt(path[1].substring(1)), |
| 57 | + model = e.base[index], |
| 58 | + node = model._ref; |
| 59 | + |
| 60 | + if(node) { |
| 61 | + var fields = node.querySelectorAll('[name]'); |
| 62 | + for(var i=0; i<fields.length; i++) { |
| 63 | + var field = fields[i], |
| 64 | + name = field.getAttribute('name'); |
| 65 | + if(model[name]) field.setAttribute('value', model[name]); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }, |
| 70 | + |
| 71 | + // Model -> DOM |
| 72 | + // DOM -> Model |
| 73 | + // Push onto DOM with empty state |
| 74 | + // Pop off DOM |> (DOM -> Model) |
| 75 | + // Model -> DOM |> Push multiple onto DOM |
| 76 | + // Modify DOM in place |
| 77 | + |
| 78 | + // Add log |
| 79 | + // Remove log |
| 80 | + // Change log |
37 | 81 |
|
38 | 82 | ready: function() { |
39 | 83 | var templateTag = this.queryEffectiveChildren('template'); |
40 | 84 | this.set('template', templateTag.innerHTML); |
41 | | - if(!this.data || this.data.length === 0) this._addRow(); |
42 | 85 | }, |
43 | 86 |
|
44 | | - _handleDataChanged: function(newData) { |
45 | | - this.async(function() { |
46 | | - newData.forEach(function(record) { |
47 | | - var node = record._ref; |
48 | | - if(node) { |
49 | | - var fields = node.querySelectorAll('[name]'); |
50 | | - for(var i=0; i<fields.length; i++) { |
51 | | - var field=fields[i]; |
52 | | - var name=field.getAttribute('name'); |
53 | | - field.setAttribute('value',record[name]); |
54 | | - } |
55 | | - } |
56 | | - }); |
57 | | - }.bind(this)); |
| 87 | + validate: function() { |
| 88 | + this.data.forEach(function(item, index) { |
| 89 | + |
| 90 | + var valid = true; |
| 91 | + |
| 92 | + if(typeof item.validation === 'function') { |
| 93 | + // Custom validation provided: call validation, passing name:value pairs as arguments |
| 94 | + var elems = item._ref.querySelectorAll('[name]'), |
| 95 | + rowData = Object.keys(elems).map(function(key) { return {name: elems[key].name, value: elems[key].value }; }); |
| 96 | + valid = item.validation.apply(rowData); |
| 97 | + } else { |
| 98 | + // Default validation: call validate on each form element and fold them together |
| 99 | + var fields = item._ref.querySelectorAll('[validation]'); |
| 100 | + valid = Object.keys(fields).reduce(function(sum, elt) { |
| 101 | + return sum && (!fields[elt].validate || fields[elt].validate(fields[elt].value)); |
| 102 | + }, true); |
| 103 | + } |
| 104 | + |
| 105 | + // Reflect validation to the model for error messaging |
| 106 | + this.set('data.'+index+'.error', !valid); |
| 107 | + |
| 108 | + }, this); |
58 | 109 | }, |
59 | 110 |
|
60 | 111 | _updateModel: function(e) { |
61 | 112 | var target = Polymer.dom(e).localTarget, |
62 | 113 | name = target.name || target.getAttribute('name'), |
63 | 114 | value = target.value || target.getAttribute('value'); |
| 115 | + |
| 116 | + console.log('_updateModel triggered'); |
64 | 117 | if(name && value) { |
65 | 118 | var index = this.$.repeater.indexForElement(target); |
66 | 119 | this.set('data.'+(index)+'.'+name, value); |
|
0 commit comments