Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
trim templates to prevent conflicts with jQuery
  • Loading branch information
SBoudrias committed Mar 22, 2013
1 parent 2aab4ed commit 725d3bb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions backbone.layoutmanager.js
Expand Up @@ -422,6 +422,7 @@ var LayoutManager = Backbone.View.extend({
function applyTemplate(rendered) {
// Actually put the rendered contents into the element.
if (rendered) {
rendered = $.trim(rendered);
// If no container is specified, we must replace the content.
if (manager.noel) {
// Hold a reference to created element as replaceWith doesn't return new el.
Expand Down
3 changes: 3 additions & 0 deletions node/index.js
Expand Up @@ -14,6 +14,9 @@ var $ = require("cheerio");
// `Backbone.View#setElement`.
$.prototype.unbind = $.prototype.off = function() { return this; };

// Set a basic trim function to allow trimming whitespace from a template
$.trim = function(str) { return str.trim(); };

// Since jQuery is not being used and LayoutManager depends on a Promise
// implementation close to jQuery, we use `underscore.deferred` here which
// matches jQuery's Deferred API exactly.
Expand Down
19 changes: 19 additions & 0 deletions test/views.js
Expand Up @@ -1940,4 +1940,23 @@ test("trigger callback on a view with `keep: true`", 1, function() {
layout.removeView();
});

// https://github.com/tbranyen/backbone.layoutmanager/issues/323
test("templates should be trimmed before insertion", 1, function() {
var layout = new Backbone.Layout({
template: "tpl",
el: false,
fetch: function() {
return "\n <div>Hey</div>\n ";
},
render: function( tpl ) {
return tpl;
}
});

layout.render();

equal(layout.$el.text(), "Hey");

});

})(typeof global !== "undefined" ? global : this);

0 comments on commit 725d3bb

Please sign in to comment.