Skip to content

Commit 9e36496

Browse files
cuberootalexmirea
authored andcommitted
Make the byContentVal model decorator update (#15)
* Make the `byContentVal` model decorator update * Change indentation to spaces * Change indentation to use spaces
1 parent f938ad2 commit 9e36496

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/dom-model/DOMDecorators.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ let byContentVal = makeDecorator(function() {
7070
if (target.addProperty) {
7171
descriptor.writable = true;
7272
target.addProperty(key, (element) => {
73-
return element && element.innerText;
73+
let valueFn = () => {
74+
return element && element.innerText;
75+
}
76+
attachContentListObserver(target, key, element, valueFn, {
77+
attributes: false, characterData: false, childList: true, subtree: false
78+
});
79+
return valueFn();
7480
});
7581
}
7682
}
@@ -159,9 +165,8 @@ let byBooleanAttrVal = makeDecorator(function(attrName) {
159165
* @param {DOMModel} target - the dom model we are attaching to
160166
* @param {String} key - the name of the property we are currently on
161167
* @param {HTMLElement} element - the element that we are parsing
162-
* @param {HTMLElement} child - the element we are observing
163-
* @param {Object} observeOptions - the options passed to observe method
164168
* @param {function} valueFn - the function to return the value of the child
169+
* @param {Object} observeOptions - the options passed to observe method
165170
*/
166171
function attachContentListObserver(target, key, element, valueFn, observeOptions ) {
167172
if (element && !element._isObserved) {

test/dom-model/DOMModelTest.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,41 @@ describe("DOMModel", () => {
349349
})
350350
});
351351

352+
describe("byContentVal", () => {
353+
354+
class ButtonModel extends DOMModel {
355+
@byAttrVal() variant;
356+
@byContentVal() label;
357+
}
358+
359+
beforeEach(() => {
360+
let result = makeModel(ButtonModel, `
361+
<my-button variant='action'>Push Me</my-button>
362+
`);
363+
element = result.element;
364+
model = result.model;
365+
});
366+
367+
it("parses the model", () => {
368+
expect(model.variant).to.equal('action');
369+
expect(model.label).to.equal('Push Me');
370+
});
371+
372+
it("updates when the DOM changes", (done) => {
373+
expect(model.variant).to.equal('action');
374+
expect(model.label).to.equal('Push Me');
375+
376+
element.addEventListener("_updateModel", (event) => {
377+
let change = event.detail[0];
378+
expect(change.propertyName).to.equal('label');
379+
expect(change.value).to.equal('New Label');
380+
done();
381+
});
382+
383+
element.innerHTML = 'New Label';
384+
});
385+
});
386+
352387
describe("byContent", () => {
353388
let element, model;
354389

0 commit comments

Comments
 (0)