Sunday 15 September 2013

Backbone.js view uses instance of self to render children. Is that okay? -



Backbone.js view uses instance of self to render children. Is that okay? -

i'm building node tree of nodes can have kid nodes , kid nodes can have grandchildren , on , forth ...

i can away using single view in turn render children listed in it's model. have looks this:

define(["backbone", "text!templates/node-tree/node.html"], function(backbone, nodetmpl) { var nodeview; homecoming nodeview = backbone.view.extend({ classname: "node", template: _.template(nodetmpl), initialize: function() { this.render(); }, render: function() { this.$el.html(this.template(this.model.tojson())); if (this.model.get("children")) { this.renderchildren(); } }, renderchildren: function() { var container = this.el.queryselector(".child-nodes"); (var = 0; < this.model.get("children").length; i++) { var kid = this.model.get("children")[i]; var view = new nodeview({model: child}); container.appendchild(view.el); } homecoming this; } }); });

now, ok? mean see working, setting self nasty surprises downwards road? other alternative have 2 identical view classes (nodea, nodeb) , have them utilize each other rendering children respectively. however, not dry , if don't need go route rather not.

it's fine that. similar marionette's compositeview. marionette's composite's default behavior (if itemview/childview property isn't defined) utilize composite view children view.

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.compositeview.md

i have used construction myself. had hierarchical info object , needed rendered children view same container view (with css tweaks).

backbone.js view architecture

No comments:

Post a Comment