From 5294d816d60b0210cd3f5cdcc17539079d7d53fb Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 29 Dec 2014 11:50:50 -0500 Subject: [PATCH 1/3] Added support for any element with contenteditable attribute set to true to provide 2-way binding. --- example/index.html | 4 ++-- src/DataBind.js | 12 +++++++++++- test/DataBind.spec.js | 19 ++++++++++++++++++- test/index.html | 3 ++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/example/index.html b/example/index.html index f059d4a..20fed14 100644 --- a/example/index.html +++ b/example/index.html @@ -83,7 +83,7 @@

Elements

key: k8 - ss + ss
@@ -127,4 +127,4 @@

Console

- \ No newline at end of file + diff --git a/src/DataBind.js b/src/DataBind.js index 55b5ce6..40314c0 100755 --- a/src/DataBind.js +++ b/src/DataBind.js @@ -481,11 +481,18 @@ return result; } + if (el.attributes && el.attributes.contenteditable && el.attributes.contenteditable.value === "true") { + if (isSetter) { + el.innerHTML = newVal; + } + return el.innerHTML; + } + // else assume non-input element if (isSetter) { el.innerText = newVal; } - return el.innerText; + return el.innerHTML; } /** @@ -521,6 +528,9 @@ if (['text', 'textarea'].indexOf(el.type) >= 0) { return 'input'; } + if (el.attributes && el.attributes.contenteditable && el.attributes.contenteditable.value === "true") { + return 'keyup'; + } } /** diff --git a/test/DataBind.spec.js b/test/DataBind.spec.js index fe357fc..5ee3d72 100644 --- a/test/DataBind.spec.js +++ b/test/DataBind.spec.js @@ -156,6 +156,23 @@ describe("DataBind", function() { expect( $(elem).text() ).toBe( 'value2' ); }); + it("should span (inline el) be 2-way bound if it has contenteditable", function() { + var elem = document.getElementById('spanEditable'); + $(elem).attr('data-key', 'k1'); + var model = {k1: 'value1'}; + DataBind.bind(elem, model); + expect( $(elem).text() ).toBe( model.k1 ); + + model.k1 = 'value2'; + expect( $(elem).text() ).toBe( 'value2' ); + + $(elem).text('changed via elem.'); + // simulate as if the change was a user input + fireEvent(elem, 'keyup'); + expect( model.k1 ).toBe( 'changed via elem.' ); + + }); + it("should email be 2-way bound", function() { var elem = document.createElement('input'); $(elem).attr('type', 'email'); @@ -615,4 +632,4 @@ describe("DataBind", function() { }); -}); \ No newline at end of file +}); diff --git a/test/index.html b/test/index.html index b5d4116..cc95355 100644 --- a/test/index.html +++ b/test/index.html @@ -60,6 +60,7 @@
dd
ss + ss ss2
@@ -73,4 +74,4 @@
- \ No newline at end of file + From a5e09aeaee29cf0ba4da2dd3aba3192851235749 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 29 Dec 2014 11:55:06 -0500 Subject: [PATCH 2/3] change the setting from setting innerHTML to innerText to be on the safe side. --- src/DataBind.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataBind.js b/src/DataBind.js index 40314c0..245218e 100755 --- a/src/DataBind.js +++ b/src/DataBind.js @@ -483,7 +483,7 @@ if (el.attributes && el.attributes.contenteditable && el.attributes.contenteditable.value === "true") { if (isSetter) { - el.innerHTML = newVal; + el.innerText = newVal; } return el.innerHTML; } From 59d58edeecdfc8c2edcf7ccb481afd34591017d6 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 29 Dec 2014 12:39:22 -0500 Subject: [PATCH 3/3] using innerText instead of innerHTML does not seem to work. The content is being set to empty string. --- src/DataBind.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DataBind.js b/src/DataBind.js index 245218e..0b1b6d3 100755 --- a/src/DataBind.js +++ b/src/DataBind.js @@ -483,7 +483,7 @@ if (el.attributes && el.attributes.contenteditable && el.attributes.contenteditable.value === "true") { if (isSetter) { - el.innerText = newVal; + el.innerHTML = newVal; } return el.innerHTML; } @@ -492,7 +492,7 @@ if (isSetter) { el.innerText = newVal; } - return el.innerHTML; + return el.innerText; } /**