Skip to content

Commit

Permalink
Merge pull request #1004 from bitovi/988-fix-else-unless
Browse files Browse the repository at this point in the history
Fixing unless helper to work with else. Fixes #988
  • Loading branch information
Curtis Cummings committed May 20, 2014
2 parents c2dd64d + b1451c4 commit a36a256
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
7 changes: 4 additions & 3 deletions view/mustache/mustache.js
Expand Up @@ -1866,9 +1866,10 @@ steal('can/util',
* {{/unless}}
*/
'unless': function (expr, options) {
if (!Mustache.resolve(expr)) {
return options.fn(options.contexts || this);
}
var fn = options.fn;
options.fn = options.inverse;
options.inverse = fn;
return Mustache._helpers['if'].fn.apply(this, arguments);
},

// Implements the `each` built-in helper.
Expand Down
6 changes: 6 additions & 0 deletions view/mustache/mustache_test.js
Expand Up @@ -3789,4 +3789,10 @@ steal("can/model", "can/view/mustache", "can/test", "can/view/mustache/spec/spec
.render({}, t.helpers), t.expected);
});

test("{{else}} with {{#unless}} (#988)", function(){
var tmpl = "<div>{{#unless noData}}data{{else}}no data{{/unless}}</div>";

var frag = can.mustache(tmpl)({ noData: true });
equal(frag.childNodes[0].innerHTML, 'no data', 'else with unless worked');
});
});
7 changes: 4 additions & 3 deletions view/stache/mustache_helpers.js
Expand Up @@ -81,9 +81,10 @@ steal("can/util", "./utils.js","can/view/live",function(can, utils, live){
}
},
'unless': function (expr, options) {
if (!resolve(expr)) {
return options.fn(options.scope || this);
}
var fn = options.fn;
options.fn = options.inverse;
options.inverse = fn;
return helpers['if'].apply(this, arguments);
},
'with': function (expr, options) {
var ctx = expr;
Expand Down
7 changes: 7 additions & 0 deletions view/stache/stache_test.js
Expand Up @@ -3460,4 +3460,11 @@ steal("can/view/stache", "can/view","can/test","can/view/mustache/spec/specs",fu
frag = can.stache(t.template)({}, t.helpers);
equal(frag.childNodes[0].nodeValue, t.expected);
});

test("{{else}} with {{#unless}} (#988)", function(){
var tmpl = "<div>{{#unless noData}}data{{else}}no data{{/unless}}</div>";

var frag = can.stache(tmpl)({ noData: true });
equal(frag.childNodes[0].innerHTML, 'no data', 'else with unless worked');
});
});

0 comments on commit a36a256

Please sign in to comment.