Last active
January 14, 2016 21:23
-
-
Save howardroark/dd818d44ecaffbccc40a to your computer and use it in GitHub Desktop.
TemplateView gives template designers more control without the need to get into the code.
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
Backbone.Marionette.TemplateView = Backbone.Marionette.ItemView.extend({ | |
templateEl: false, | |
inlineTemplate: false, | |
render: function() { | |
if(this.inlineTemplate) { | |
this.templateEl = true; | |
this.templateConfig = jQuery(this.inlineTemplate).data(); | |
this.template = Backbone.Marionette.TemplateCache.get(this.inlineTemplate); | |
} | |
this._ensureViewIsIntact(); | |
this.triggerMethod('before:render', this); | |
this._renderTemplate(); | |
this.isRendered = true; | |
this.bindUIElements(); | |
this.triggerMethod('render', this); | |
return this; | |
}, | |
_renderTemplate: function() { | |
var template = this.getTemplate(); | |
// Allow template-less item views | |
if (template === false) { | |
return; | |
} | |
if (!template) { | |
throw new Marionette.Error({ | |
name: 'UndefinedTemplateError', | |
message: 'Cannot render the template since it is null or undefined.' | |
}); | |
} | |
// Add in entity data and template helpers | |
var data = this.mixinTemplateHelpers(this.serializeData()); | |
if(this.templateEl) { | |
// Render and set element | |
var html = Marionette.Renderer.render(template, data, this); | |
this.setElement(jQuery(html)); | |
} else { | |
// Render and add to el | |
var html = Marionette.Renderer.render(template, data, this); | |
this.attachElContent(html); | |
} | |
if(this.inlineTemplate) { | |
if(this.templateConfig.prependTo) { | |
jQuery(this.el).prependTo(this.templateConfig.prependTo); | |
} | |
if (this.templateConfig.appendTo) { | |
jQuery(this.el).appendTo(this.templateConfig.appendTo); | |
} | |
} | |
return this; | |
} | |
}); |
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
var HeaderView = Backbone.Marionette.TemplateView.extend( { | |
inlineTemplate : "#headerTemplate", | |
templateHelpers: function() { | |
return { | |
title: 'TemplateView' | |
} | |
} | |
} ); |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<body> | |
<script type="text/template" data-prepend-to="body" id="headerTemplate"> | |
<header class="bar bar-nav"> | |
<h1 class="title"><%= title %></h1> | |
</header> | |
</script> | |
<script src="/vendor/jquery/dist/jquery.min.js"></script> | |
<script src="/vendor/underscore/underscore.js"></script> | |
<script src="/vendor/backbone/backbone.js"></script> | |
<script src="/vendor/backbone.marionette/lib/backbone.marionette.js"></script> | |
<script src="/js/Backbone.Marionette.TemplateView.js"></script> | |
<script src="/js/script.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment