From 2e32966ce9f20633c75428cefccfaa5054e37a60 Mon Sep 17 00:00:00 2001 From: Eric Hemesath Date: Thu, 28 Mar 2013 15:01:40 -0500 Subject: [PATCH 1/2] Allow HTML string to be passed as context parameter IF context parameter is a string, THEN create a DIV AND insert context into that DIV AND reassign context = DIV --- src/transparency.coffee | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/transparency.coffee b/src/transparency.coffee index 530aff23..bccf5d4d 100644 --- a/src/transparency.coffee +++ b/src/transparency.coffee @@ -42,6 +42,12 @@ Transparency.render = (context, models = [], directives = {}, options = {}) -> return unless context models = [models] unless _.isArray models + # If context was passed in as an HTML string, then convert it to a div element with innHTML as passed in HTML string. + if typeof context == 'string' + _html_string = context + context = document.createElement('div') + context.innerHTML = _html_string + # Context element, state and functionality is wrapped to `Context` object. Get it, or create a new # if it doesn't exist yet. context = helpers.data(context).context ||= new Context(context, Transparency) From ce0b9bbfde187822583dd2080f6eb889dbaf3d80 Mon Sep 17 00:00:00 2001 From: Eric Hemesath Date: Fri, 29 Mar 2013 09:44:17 -0500 Subject: [PATCH 2/2] Added a test for passing context parameter to render as HTML String --- spec/basicsSpec.coffee | 8 ++++++++ spec/basicsSpec.js | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/spec/basicsSpec.coffee b/spec/basicsSpec.coffee index 4876c611..ad929f76 100644 --- a/spec/basicsSpec.coffee +++ b/spec/basicsSpec.coffee @@ -62,6 +62,14 @@ describe "Transparency", -> template.render data expect(template).toBeEqual expected + it "should accept context as a string", -> + template = '
' + data = hello: 'Hello' + expected = $ '
Hello
' + + template = $ window.Transparency.render template, data + expect(template).toBeEqual expected + it "should match model keys to template by element id, class, name attribute and data-bind attribute", -> template = $ """
diff --git a/spec/basicsSpec.js b/spec/basicsSpec.js index 353c17a5..7d22b11f 100644 --- a/spec/basicsSpec.js +++ b/spec/basicsSpec.js @@ -38,6 +38,17 @@ template.render(data); return expect(template).toBeEqual(expected); }); + it("should accept context as a string", function() { + var data, expected, template; + + template = '
'; + data = { + hello: 'Hello' + }; + expected = $('
Hello
'); + template = $(window.Transparency.render(template, data)); + return expect(template).toBeEqual(expected); + }); it("should match model keys to template by element id, class, name attribute and data-bind attribute", function() { var data, expected, template;