Permalink
Browse files

Merge pull request #258 from bitovi/mustache-helper-priority-258

can.Observe.prototype.each overrides {{#each}} Mustache helper.
  • Loading branch information...
daffl committed Mar 25, 2013
2 parents 0ede8b6 + c6a3bab commit d33de3f8d26a59084ee140379c5465891b1d8106
Showing with 29 additions and 9 deletions.
  1. +7 −6 view/mustache/mustache.js
  2. +22 −3 view/mustache/test/mustache_test.js
View
@@ -643,7 +643,7 @@ function( can ){
opts.hash = args.pop()[HASH];
}
args.push(opts);
// Call the helper.
return helper.fn.apply(context, args) || '';
}
@@ -831,19 +831,20 @@ function( can ){
}
}
}
if( defaultObserve &&
// if there's not a helper by this name and no attribute with this name
!(Mustache.getHelper(ref) &&
can.inArray(defaultObserveName, can.Observe.keys(defaultObserve)) === -1) ) {
return defaultObserve.compute(defaultObserveName);
}
// Support helper-like functions as anonymous helpers
if (typeof obj !== 'undefined' && obj !== null && can.isFunction(obj[ref])) {
return obj[ref];
}
// Support helpers without arguments, but only if there wasn't a matching data reference.
else if (value = Mustache.getHelper(ref,options)) {
// Helpers have priority over local function, see https://github.com/bitovi/canjs/issues/258
if (value = Mustache.getHelper(ref,options)) {
return ref;
} else if (typeof obj !== 'undefined' && obj !== null && can.isFunction(obj[ref])) {
// Support helper-like functions as anonymous helpers
return obj[ref];
}
return '';
@@ -1753,7 +1753,7 @@ test("Null properties do not throw errors in Mustache.get", function() {
div2.appendChild(frag2);
equal(div.innerHTML, "Foo bar does not exist");
equal(div2.innerHTML, "Foo bar exists");
})
});
// Issue #288
test("Data helper should set proper data instead of a context stack", function() {
@@ -1790,7 +1790,7 @@ test("Data helper should set proper data instead of a context stack", function()
div.appendChild(renderer3(data));
span = can.$(div.getElementsByTagName('span')[0]);
strictEqual(can.data(span, 'attr'), data.bar, 'Nested data 3 should have correct data');
})
});
// Issue #333
test("Functions passed to default helpers should be evaluated", function() {
@@ -1807,6 +1807,25 @@ test("Functions passed to default helpers should be evaluated", function() {
div.appendChild(renderer(data));
span = can.$(div.getElementsByTagName('span')[0]);
equal(div.innerHTML, 'No ducks!', 'The function evaluated should evaluate false');
})
});
test("Helpers always have priority (#258)", function() {
can.Mustache.registerHelper('callMe', function(arg) {
return arg + ' called me!';
});
var t = {
template: "<div>{{callMe 'Tester'}}</div>",
expected: "<div>Tester called me!</div>",
data: {
callMe: function(arg) {
return arg + ' hanging up!';
}
}
};
var expected = t.expected.replace(/&quot;/g, '&#34;').replace(/\r\n/g, '\n');
same(new can.Mustache({ text: t.template }).render(t.data), expected);
});
});

0 comments on commit d33de3f

Please sign in to comment.