Skip to content
Browse files

Fix for rendering falsey values: Issue #31:

  • Loading branch information...
1 parent 21da66a commit 34f9affe45c7dc33187977d54407ff1fc131c3bc @BorisMoore committed
Showing with 10 additions and 8 deletions.
  1. +2 −3 jsrender.js
  2. +8 −5 test/unit/jsrender-tests-no-jquery.js
View
5 jsrender.js
@@ -171,7 +171,7 @@ function View( context, path, parentView, data, template, index ) {
tmpl: template,
path: path,
parent: parentView,
- data : data||{},
+ data: data,
ctx: context,
views: $.isArray( data ) ? [] : {}
};
@@ -282,7 +282,6 @@ function renderContent( data, context, parentView, path, index ) {
path = path || self.path;
index = index || self.index;
props = self.props;
-// data = data === undefined ? parentView.data : data;
} else {
tmpl = self.jquery && self[0] // This is a call $.fn.render
|| self; // This is a call from tmpl.render
@@ -323,7 +322,7 @@ function renderContent( data, context, parentView, path, index ) {
for ( i = 0, l = data.length; i < l; i++ ) {
// Create a view for each data item.
dataItem = data[ i ];
- itemResult = tmpl.fn( dataItem || {}, View( context, path, newView, dataItem, tmpl, (index||0) + i ));
+ itemResult = tmpl.fn( dataItem, View( context, path, newView, dataItem, tmpl, (index||0) + i ), jsv );
result += itemWrap ? itemWrap( itemResult, props ) : itemResult;
}
} else {
View
13 test/unit/jsrender-tests-no-jquery.js
@@ -33,7 +33,6 @@ var person = { name: "Jo" },
var tmplString = "A_{{:name}}_B";
jsviews.tags({ sort: sort });
-//jsviews.allowCode = true;
module( "tagParser" );
test("{{if}} {{else}}", function() {
expect(3);
@@ -179,7 +178,7 @@ test("expressions", function() {
module( "{{for}}" );
test("{{for}}", function() {
- expect(8);
+ expect(9);
jsviews.templates( {
forTmpl: "header_{{for people}}{{:name}}{{/for}}_footer",
layoutTmpl: {
@@ -187,7 +186,8 @@ test("{{for}}", function() {
layout: true
},
pageTmpl: '{{for people tmpl="layoutTmpl"/}}',
- simpleFor: "a{{for people}}Content{{/for}}b"
+ simpleFor: "a{{for people}}Content{{/for}}b",
+ forPrimitiveDataTypes: "a{{for people}}{{:#data}}{{/for}}b"
});
equal( jsviews.render.forTmpl({ people: people }), "header_JoBill_footer", '{{for people}}...{{/for}}' );
@@ -199,6 +199,7 @@ test("{{for}}", function() {
equal( jsviews.render.simpleFor({people:[null,undefined,1]}), "aContentContentContentb", 'null or undefined members of array are also rendered' );
equal( jsviews.render.simpleFor({people:null}), "ab", 'null is rendered as empty string' );
equal( jsviews.render.simpleFor({}), "ab", 'undefined is rendered as empty string' );
+ equal( jsviews.render.forPrimitiveDataTypes({people:[0,1,"abc","",,true,false]}), "a01abctruefalseb", 'Primitive types render correctly, even if falsey' );
});
module( "api" );
@@ -255,14 +256,15 @@ test("templates", function() {
});
test("render", function() {
- expect(13);
+ expect(14);
var tmpl1 = jsviews.templates( "myTmpl8", tmplString );
jsviews.templates( {
simple: "Content",
simpleLayout: {
markup: "Content",
layout: true
- }
+ },
+ primitiveDataTypes: "{{:#data}}"
});
equal( tmpl1.render( person ), "A_Jo_B", 'tmpl1.render( data );' );
@@ -282,6 +284,7 @@ test("render", function() {
equal( jsviews.render.simpleLayout([]), "Content", 'Layout renders once, for empty array' );
equal( jsviews.render.simpleLayout(null), "Content", 'Layout renders once, for null' );
equal( jsviews.render.simpleLayout(), "Content", 'Layout renders once, for undefined' );
+ equal( jsviews.render.primitiveDataTypes([0,1,"abc","",,true,false]), "01abctruefalse", 'Primitive types render correctly, even if falsey' );
});
test("converters", function() {

0 comments on commit 34f9aff

Please sign in to comment.
Something went wrong with that request. Please try again.