Skip to content

Commit

Permalink
Adding text-binding on
Browse files Browse the repository at this point in the history
  • Loading branch information
eavichay committed Dec 1, 2016
1 parent 9df22a7 commit c885792
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
14 changes: 12 additions & 2 deletions example/Todo/TodoDemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,29 @@ <h4>Data-Binding</h4>
&lt;/login-screen>
</code>
</pre>
<h4>Text-to-Data-Binding</h4>
<p>Inject property values into your text with a simple annotation. Have no fear from performance issues: you choose where and when your bindings happen!</p>
<pre><code class="javascript">
&lt;my-element user="{UserModel}"}>
&lt;span bind>
Hello, [[user.name]]! Welcome to [[location]]
&lt;/span>
&lt;/my-element>
</code>
</pre>
<h4>Dependency Injection</h4>
<p>Dependency injection using curly braces</p>
<pre><code class="javascript">
const instance = new TodoModel()

SlimInjector.define('loginModel', function() {
SlimInjector.define('LoginModel', function() {
"use strict";
return instance
})

...

&lt;login-details model="{loginModel}" />
&lt;login-details model="{LoginModel}" />
</code>
</pre>
</s-container>
Expand Down
8 changes: 3 additions & 5 deletions example/Todo/todo-task-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
get template() {
return `
<div id="container">
<span id="number">$NUM</span>
<span id="number" bind>[[data.todoId]]</span>
<input type="checkbox" done="[done]"/>
<span id="title" text="[name]">$NAME</span>
<span id="title" text="[name]" bind>[[data.name]]</span>
<input type="button" value="X">
</div>`
}
Expand All @@ -26,9 +26,7 @@
}

update () {
this.innerHTML = this.innerHTML
.split('$NAME').join(this.data.name)
.split('$NUM').join(this.data.todoId)
super.update()

var checkbox = this.find('input[type=checkbox]')
var delButton = this.find('input[value="X"]')
Expand Down
24 changes: 23 additions & 1 deletion lib/SlimElement.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@
render() {}
afterRender() {}
onAdded() {}
update() {}
update() {
this._applyTextBindings()
}
onRemoved() {}

//noinspection JSUnusedGlobalSymbols
Expand Down Expand Up @@ -161,6 +163,26 @@
this.afterRender()
}

_applyTextBindings() {
const x = function getDescendantProp(obj, desc) {
var arr = desc.split(".");
while(arr.length && obj) {
obj = obj[arr.shift()]
};
return obj;
}

for (let child of this.querySelectorAll('*[bind]')) {
var match = child.textContent.match(/\[\[([\w|.]+)\]\]/g)
child.sourceTextContent = child.textContent;
if (match) {
for (var i = 0; i < match.length; i++) {
child.innerText = child.innerText.replace(match[i], x(this, match[i].match(/([^\[].+[^\]])/)[0]))
}
}
}
}

//noinspection JSUnusedGlobalSymbols
attachedCallback() {
this.dispatchEvent(new Event('elementAdded', {bubbles:true}))
Expand Down

0 comments on commit c885792

Please sign in to comment.