Skip to content

Commit

Permalink
Mustache helpers always have priority
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Mar 25, 2013
1 parent 0ede8b6 commit c6a3bab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
13 changes: 7 additions & 6 deletions view/mustache/mustache.js
Expand Up @@ -643,7 +643,7 @@ function( can ){
opts.hash = args.pop()[HASH];
}
args.push(opts);

// Call the helper.
return helper.fn.apply(context, args) || '';
}
Expand Down Expand Up @@ -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 '';
Expand Down
25 changes: 22 additions & 3 deletions view/mustache/test/mustache_test.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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 c6a3bab

Please sign in to comment.