-
-
Save jazzdan/1585752 to your computer and use it in GitHub Desktop.
Backbone-Handlebars View Template
This file contains hidden or 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
/* base view template */ | |
var ViewTemplate = Backbone.View.extend({ | |
// containing element | |
el: $('body'), | |
// events | |
events: {}, | |
// initialize | |
initialize: function(){ | |
_.bindAll(this, 'render', 'template', 'html'); | |
throw "Error: trying to instantiate ViewTemplate - templates must be overridden sir" | |
}, | |
// this overwrites all content in the element - override with append() if thats not what you're going for | |
render: function(){ | |
$(this.el).html(this.html()); | |
}, | |
// id of the handlebars template element | |
template_id: '#template', | |
// template rendering function | |
template: function(id, context){ | |
var src = $( id || this.template_id ).html(); | |
var templ = Handlebars.compile(src); | |
return templ(context || this.context()); | |
}, | |
// context function | |
context: function(){ | |
var that = this; | |
// returns a handlebars context object | |
return { | |
title: 'Template View' | |
} | |
}, | |
// returns the html for the view | |
html: function(){ | |
return this.template(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment