From 2ea7049dd26b13edff42a1b28527db40f2809387 Mon Sep 17 00:00:00 2001 From: Jeremy Likness Date: Wed, 27 Nov 2019 14:26:18 -0800 Subject: [PATCH] Add declarative data-binding --- js/dataBinding.js | 24 ++++++++++++++++++++++++ js/slide.js | 6 +++--- slides/data-binding.html | 39 ++++++++++++++++++--------------------- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/js/dataBinding.js b/js/dataBinding.js index 1f09d4d..bcede41 100644 --- a/js/dataBinding.js +++ b/js/dataBinding.js @@ -71,6 +71,30 @@ export class DataBinding { }; } + /** + * + * @param {HTMLElement} elem The parent element + * @param {object} context The context to use for binding + */ + bindAll(elem, context) { + this.bindLists(elem, context); + this.bindObservables(elem, context); + } + + /** + * Searches for "data-bind" attribute to data-bind observables + * @param {HTMLElement} elem The parent element to search + * @param {object} context The context to use for binding + */ + bindObservables(elem, context) { + const dataBinding = elem.querySelectorAll("[data-bind]"); + dataBinding.forEach(elem => { + this.bindValue( + /** @type {HTMLInputElement} */(elem), + context[elem.getAttribute("data-bind")]); + }); + } + /** * Searches for "repeat" attribute to data-bind lists * @param {HTMLElement} elem The parent element to search diff --git a/js/slide.js b/js/slide.js index f0f4688..fe7c98b 100644 --- a/js/slide.js +++ b/js/slide.js @@ -1,6 +1,6 @@ // @ts-check -import {DataBinding} from "./dataBinding.js" +import { DataBinding } from "./dataBinding.js" /** * Represents a slide @@ -21,7 +21,7 @@ export class Slide { * Context for embedded scripts * @type {object} */ - this._context = {}; + this._context = { }; /** * Data binding helper * @type {DataBinding} @@ -66,7 +66,7 @@ export class Slide { const script = this._html.querySelector("script"); if (script) { this._dataBinding.executeInContext(script.innerText, this._context, true); - this._dataBinding.bindLists(this._html, this._context); + this._dataBinding.bindAll(this._html, this._context); } } diff --git a/slides/data-binding.html b/slides/data-binding.html index a6b6308..fd0d1af 100644 --- a/slides/data-binding.html +++ b/slides/data-binding.html @@ -1,30 +1,27 @@ Data-Binding Example 1

Data-Binding Example

- - - - - - + + + + + +
\ No newline at end of file