在视图中查看嵌套骨干JS [英] Nesting Views within Views in backbone js

查看:117
本文介绍了在视图中查看嵌套骨干JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Backbone.js的建立一些复杂的视图关系工作,如果有从做一些看起来像这样的JavaScript性能的角度来看的任何问题我想知道:

I'm working with backbone.js building some complex view relationships, and I'm wondering if there are any problems from a javascript performance standpoint of doing something that looks like this:

var viewOne = Backbone.View.extend({
         tagName : 'li',
         initialize : function() {
              this.v2 = new viewTwo({parent:this});
         },
         clickHideOne : function() {
              $(this.el).removeClass('selected');
         }
});

var viewTwo = Backbone.View.extend({
         tagName : 'a',
         initialize : function() {
              this.bind('click', this.clickHide, this);
         },
         clickHide(){
              $(this.el).removeClass('selected');
              this.options.parent.clickHideOne();
         }
});

如果这是两种观点之间的循环引用的一个很简单的例子,为了在一个视图中的事件很容易传播了意见链,或维持在父视图对象的任何引用。是否有任何情况下,这将是一个问题,特别是与在IE7 + DOM元素引用的潜在泄漏,或者是有其他建议的最佳实践为参照父母的意见。

Where this is a very simple example of a circular reference between two views, in order to have events in one view easily propagate up a chain of views, or maintain any references to objects in the parent views. Are there any situations where this would be a problem, specifically in relation to the potential leaks with DOM element references in IE7+, or is there another recommended best practice for referencing parent views.

另外,我明白,我可能只是这样做$(this.el).parent(礼)removeClass移除('选择')。在viewTwo,这不是问题的关键......这只是我对循环引用的问题的一个很简单的例子。

Also, I understand that I could just do $(this.el).parent('li').removeClass('selected'); in viewTwo, that's not the point... this just a very simple example of the question I have about the circular reference.

推荐答案

有负责子视图父视图是不是一个坏主意,是骨干一个相当常见的场景。我与上面的code看到的问题是,子视图都有父视图的知识的事实。我会建议使用在viewTwo自定义事件,并有viewOne绑定到这些事件,然后作出相应的反应。

Having a parent view responsible for child views is not a bad idea and is quite a common scenario in backbone. The problem I see with the code above is the fact that the child view has knowledge of its parent view. I would suggest using custom events in the viewTwo and have viewOne bind to those events and then respond accordingly.

这是与骨干很容易通过使用触发器()方法和bind()方法。

This is quite easy with backbone by using the trigger() method and the bind() method.

这篇关于在视图中查看嵌套骨干JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆