backbone.js - Backbone templates: Using HBS to dynamically switch templates -



backbone.js - Backbone templates: Using HBS to dynamically switch templates -

i found this example on here of how utilize hbs plug-in manage templates. seems great solution. @machineghost suggests using requirejs include templates this:

define(['template!path/to/sometemplate'], function(sometemplate) { var mynewview = baseview.extend({template: sometemplate}); $('body').append(new mynewview().render().el); }

this great, except need dynamically switch templates. here illustration of 1 of views:

define([ 'jquery', 'underscore', 'backbone', 'models/tablemodel', 'collections/tablescollection', 'views/tablesview' ], function($, _, backbone, tablemodel, tablescollection, tablesview) { var t = new tablescollection(null, {url: 'applications-lab'}); homecoming new tablesview({ collection: t, template: 'applications-lab-template', url: 'applications-lab'}); });

as can seem, i'm passing in template when view rendered. i'm wondering can pass in variable define statement tell backbone template path use? i'm newbie backbone , requirejs, , not sure. suggestions anyone?

preliminary notes:

require.js not allow parameters in module definition, define accepts dependency array , definition function :

define(['dep1', 'dep2', ...], function(dep1, dep2) { }) i not define view, instantiate , inject el in same module sense free mix , match taste

let's start module defining simple view default template, let's views/templated.js

define(['backbone', 'hbs!path/to/defaulttemplate'], function(backbone, defaulttemplate) { var mynewview = backbone.view.extend({ template: defaulttemplate, initialize: function(opts) { opts = opts || {}; // utilize template defined in options or on prototype this.template = opts.template || this.template; } }); homecoming mynewview; });

now have pull view definition , optional template require:

require(['views/templated', 'hbs!path/to/anothertemplate'], function(mynewview, anothertemplate) { // view default template var v1 = new mynewview(); // view new template var v2 = new mynewview({ template: anothertemplate }); });

to create new class overridden default template, define new module (views/override.js)

define(['views/templated', 'hbs!path/to/anothertemplate'], function(mynewview, anothertemplate) { var anothernewview = mynewview.extend({ template: anothertemplate }); homecoming anothernewview; });

finally, can alter template on given instance straight assigning new value.

var v = new mynewview(); v.template = tpl;

a fiddle simulating views hierarchy : http://jsfiddle.net/nikoshr/urddr/

coming code, blocks like

require(['models/tablemodel', 'collections/tablescollection', 'views/templated', 'applications-lab-template'], function(tablemodel, tablescollection, tablesview, tpl) { var t = new tablescollection(null, {url: 'applications-lab'}); var v = new tablesview({ collection: t, template: tpl url: 'applications-lab' }); // or, if prefer , don't render in initialize v.template = tpl; });

backbone.js requirejs

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -