Skip to content

Instantly share code, notes, and snippets.

@ktusznio
Created March 8, 2013 19:52
Show Gist options
  • Save ktusznio/5119318 to your computer and use it in GitHub Desktop.
Save ktusznio/5119318 to your computer and use it in GitHub Desktop.
class BaseView extends Backbone.View
template: ""
# Render hooks.
beforeFirstRender: ->
beforeRender: ->
afterRender: ->
afterFirstRender: ->
render: =>
if !@rendered and @beforeFirstRender() is false
return
if @beforeRender() is false
return
if @template
html = @renderTemplate(@template, @model)
@$el.html html
@$el.show()
unless @rendered
@afterFirstRender()
@rendered = true
@afterRender()
@
renderTemplate: (template, model = null) ->
context =
model: model
context.helpers = @namespace.ViewHelpers?.exports or {}
context.helpers.template = @renderPartial
if model
_.extend context, model.toJSON()
@renderPartial template, context
renderPartial: (template, context) =>
JST["#{@templatesRoot}templates/#{template}"](context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment