Reputation: 8177
I get the error Object [object Array] has no method 'search'
in mustache.js line 103.
I've created an object of templates when the page is ready:
window.templates = {};
$("script[type='text/mustache']").each(function(){
window.templates[this.id] = Mustache.parse(this.innerHTML);
});
I then have a json object from my server, the relevant part of which I'm passing into a rendering function.
var render = function(data){
var content = "";
for (n in data){
content += Mustache.render(window.templates[n], data[n], window.templates);
}
return content;
}
I'm attempting to match up templates with models: the window.template
keys matching the keys in my data object returned from my model. Since templates may contain other templates as partials, I'm passing the entire templates object back in at the end. I can confirm that I'm getting the model name I expect, and that it does properly match with a template.
I'm new to Mustache, so this is probably a simple fix. What am I missing?
Thanks
Upvotes: 1
Views: 1513
Reputation: 6162
Without Mustache version I can only guess but it seems you are using version 0.7.3. If so then you are looking at wrong documentation ("Pre-parsing and Caching Templates" in master branch). And what you should be looking is "Compiled Templates" in docs for v0.7.3.
So what you should be doing is
window.templates = {};
$("script[type='text/mustache']").each(function() {
window.templates[this.id] = Mustache.compilePartial(this.id, this.innerHTML);
});
and your render()
function
function render(data) {
var content = "";
for(var n in data) {
// don't pass partials. internal cache will be used
content += window.templates[n](data[n]);
}
return content;
}
Here is plunker to play with.
Upvotes: 1