Created
February 26, 2013 22:35
-
-
Save justjohn/5042970 to your computer and use it in GitHub Desktop.
Twig.js BackBone View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can i render twig on my backbone