Skip to content

Instantly share code, notes, and snippets.

@andrewpthorp
Created March 2, 2012 16:20
Show Gist options
  • Save andrewpthorp/1959425 to your computer and use it in GitHub Desktop.
Save andrewpthorp/1959425 to your computer and use it in GitHub Desktop.
Underscore.js errors
MYAPP = {
fetchTemplate: function(path, done) {
window.JST = window.JST || {};
// Should be an instant synchronous way of getting the template, if it
// exists in the JST object.
if (JST[path]) {
return done(JST[path]);
}
// Fetch it asynchronously if not available from JST
return $.get(path, function(contents) {
var tmpl = _.template(contents);
JST[path] = tmpl;
done(tmpl);
});
}
};
SomeView = Backbone.View.extend({
id: "temp-test",
template: "template.html",
render: function(){
var self = this;
MYAPP.fetchTemplate(this.template, function(tmpl){
self.$el.html(tmpl());
if (_.isFunction(done)){
done(self.el);
}
}
}
};
<section class="benefits index">
<img id="temp" src="assets/img/benefits-main.png" />
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment