为什么在backbone.js 视图中使用bindAll? [英] why do bindAll in backbone.js views?

查看:23
本文介绍了为什么在backbone.js 视图中使用bindAll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主干的 todo demo 中,代码有几个地方 _.bindAll(this,...) 使用.具体来说,它用在两个视图的 initialize 函数中.据我所知,有必要执行以下操作:

In backbone's todo demo the code has a few spots where _.bindAll(this,...) is used. Specifically it's used in the initialize function of both views. As far as I can tell it's necessary to do the following:

this.$('.todo-content').text(content);

但是为什么要做到以上几点,当你可以做到的时候:

But why would one want to do the above, when one can do:

$('.todo-content').text(content);

?

推荐答案

this.$ 将 jQuery 的上下文限制为视图的元素,因此操作更快.

this.$ limits jQuery's context to the view's element, so operations are quicker.

此外,this.$('.todo-item') 不会在您的视图元素之外使用 todo-item 类找到您的元素.

Additionaly, this.$('.todo-item') won't find your elements with todo-item class outside your view's element.

这篇关于为什么在backbone.js 视图中使用bindAll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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