From 00fa71dd8122cbaf8472e7a6596fb5a8f72e5c3e Mon Sep 17 00:00:00 2001 From: Mihael Konjevic Date: Sat, 19 Jan 2013 13:57:08 +0100 Subject: [PATCH] Implement support for rendering document fragments returned from the renderer function in the Mustache and EJS templates --- view/view.js | 6 ++++++ view/view_test.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/view/view.js b/view/view.js index 5321f92b37a..64e499d0a09 100644 --- a/view/view.js +++ b/view/view.js @@ -56,10 +56,16 @@ steal("can/util", function( can ) { // then hook it up fragment: function(result){ var frag = can.buildFragment(result,document.body); + var dataId = $view.hook(function(el){ + el.parentNode.replaceChild(frag, el); + }) // If we have an empty frag... if(!frag.childNodes.length) { frag.appendChild(document.createTextNode('')); } + frag.toString = function(){ + return ""; + } return frag; }, diff --git a/view/view_test.js b/view/view_test.js index 1518065fc5f..e23a83bbd3b 100644 --- a/view/view_test.js +++ b/view/view_test.js @@ -300,4 +300,38 @@ form.reset(); equal(form.children[0].value, 'testing', 'Textarea value set back to original live-bound value'); }); + + test("Support passing document fragments created with can.view.fragment to templates", function(){ + var mustache = can.view.mustache('{{{ subview }}}'); + var ejs = can.view.ejs('<%== subview() %>') + + var subview = can.view.mustache('Subview {{ foo }}'); + var subviewFn = function(){ + return subview({ + foo : foo + }) + } + var foo = can.compute("FOO") + + var mustacheDiv = document.createElement('div'); + var ejsDiv = document.createElement('div'); + + mustacheDiv.appendChild(mustache({ + subview : subviewFn + })); + + ejsDiv.appendChild(ejs({ + subview : subviewFn + })); + + equal(mustacheDiv.innerHTML, "Subview FOO", "Mustache works correctly") + equal(ejsDiv.innerHTML, "Subview FOO", "EJS works correctly") + + foo("BAR") + + equal(mustacheDiv.innerHTML, "Subview BAR", "Live binding in mustache works correctly") + equal(ejsDiv.innerHTML, "Subview BAR", "Live binding in EJS works correctly") + + }) + })(); \ No newline at end of file