Skip to content

Instantly share code, notes, and snippets.

@justjohn
Created February 26, 2013 22:35
Show Gist options
  • Save justjohn/5042970 to your computer and use it in GitHub Desktop.
Save justjohn/5042970 to your computer and use it in GitHub Desktop.
Twig.js BackBone View
define(
["jquery", "backbone", "twig", "q"],
function($, Backbone, Twig, Q) {
return Backbone.View.extend({
tagName: "div"
, ready: function() {
return this.templateReady.promise;
}
// Render the template with the contents of the Setting model
, render: function() {
var that = this;
return this.ready().then(function(template) {
var el = that.el
, model = that.model
, json = model?model.toJSON():{};
$(el).html(template.render(json));
});
}
, load: function() {
var ready = Q.defer();
this.templateReady = ready;
if (!this.template)
throw "Cannot instantiate a TwigView with no template.";
Twig.twig({
href: this.template
, load: function(template) {
console.log("TwigView loaded ", template.id);
ready.resolve(template);
}
});
return this.ready();
}
// Initialize the TwigView
, initialize: function() {
var that = this;
if (!this.model)
this.model = new Backbone.Model({});
this.load()
.then(function() {
that.init && that.init();
that.model.bind("change", that.modelChanged, that);
that.render();
});
}
, modelChanged: function() {
this.render();
}
});
}
);
@jvillariaza
Copy link

how can i render twig on my backbone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment