backbone.js - Does using Backbone's dynamically created .el couple templates with the view? -
i'm learning how use backbone in project i'm working on , have question best practices when comes decoupling views , templates in backbone. in particular best use of dynamically generated view.el
, view.$el
.
in documentation says view.el is created view's tagname, classname, id , attributes properties, if specified. if not, el empty div.
question #1:
in setting view's attributes, id, , classname not highly couple view , template file point if designer wanted adjust template need access view?
for example, have a jquery mobile list attributes , class names dictate , feel of list:
<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" class="ui-btn ui-li ui-li-has-thumb ui-btn-up-c"></li>
if use tagname, attributes, , classname properties of backbone view, of stored in view file , not in template. not bad practice or missing something?
question 2
to avoid above am:
a. not using view.el
or related properties (tagname, attributes, etc.) @ , instead putting directly in template file.
b. compiling template using underscore.template()
, injecting view.$el
property calling in render() function:
view.$el.html(_.template(template, data))
and when it's root view, passing target id view constructor , in render function calling:
$(this.target).append(view.$el.html());
alternatively, if it's view calling view:
$("#target_id").append(view.render.$el.html());
is ok approach/practice or there other best practice should using?
1) el
not template (i.e. not before rendered) binding view dom
. can pass in selector
view specify el
when define view
var myawesomeview = backbone.view.extend({ el: '#some_node', ... });
or when instantiate view
var myview = new myawesomeview({ el: '#some_other_node', ... });
you can pass in $
selector in @bentrm answer well, both give pretty same result.
you can not specify selector
@ specify tagname
, classname
or id
. have @ source code snippet: https://github.com/jashkenas/backbone/blob/master/backbone.js#l1092-l1106
for example, have li
template , instead of have view attach li
have attach 1 of ul
or ol
2) think have explained a. b, have @ awesome presentation: http://amy.palamounta.in/2013/04/12/unsuck-your-backbone/
Comments
Post a Comment