Monday 15 February 2010

javascript - Backbone to use Underscore templates with helpers -



javascript - Backbone to use Underscore templates with helpers -

i want utilize helper functions underscore templates , backbone. i'm doing way:

view:

var view = backbone.view.extend({ // ... template: gettpl('#b_ezlo', 1), // ... render: function(){ this.$el.html( this.template(this.model.tojson()) ); } });

template getter: here homecoming template, along helper functions. problem when prepare cannot homecoming helper functions, because require homecoming other variables template, , causes undefined getdisabledstate:

function gettpl(tpl, options) { if (!tpl) return; if (!options) options = null; var prepare = false; if (options == 1) { // called on view initialization // template: gettpl('#b_ezlo', 1), options = {}; prepare = true; } var viewhelpers = {} if (tpl == "#b_view") { console.log("prepare", prepare); viewhelpers.getdisabledstate = function() { if (typeof options.disabled != "undefined") { homecoming options.disabled; } else { homecoming ''; } } } _.extend(options, viewhelpers); if (prepare) { homecoming _.template($(tpl).html()); } else { homecoming _.template($(tpl).html())(options); } }

and part of template (jade) want utilize helper:

.icon-block(data-disabled!="<% if (typeof getdisabledstate != 'undefined') {getdisabledstate()} %>")

what dislike here if (typeof getdisabledstate != 'undefined') part, not nice have in template.

so if there other way prepare templates helper functions?

thanks evgeniy's comment, marionettejs way - defining helpers in view.

http://marionettejs.com/docs/marionette.view.html#viewtemplatehelpers

javascript templates backbone.js underscore.js

No comments:

Post a Comment