Backbone.js 事件绑定.像“委托"一样在 Jquery 中? [英] Backbone.js Events binding. Like "delegate" in Jquery?

查看:17
本文介绍了Backbone.js 事件绑定.像“委托"一样在 Jquery 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是backbone.js 的新手.我在 Backbone.js 中看到,事件绑定:

I'm new in backbone.js. And I see that in Backbone.js, the events binding :

var PersonView = Backbone.View.extend({
    ....
    events : {
        "click button.btnSay" : "saySomething"
    },

    saySomething : function(){
        ....
    }
    ...
});

...调用时不需要button.btnSay,这和Jquery中的delegate"很像.

...doesn't need the button.btnSay to be existed in the time of calling, that's so similar to "delegate" in Jquery.

现在,问题是,我们可以将 html 页面中的任何 按钮 更改为 .btnSay(通过 Firebug 等).他们最终得到了听众.我们如何防止这种情况发生?

And now, the problem is, we can change any button in our html page to .btnSay (by Firebug, etc.). And they eventualy get the listeners. How can we prevent this?

推荐答案

它与 delegate 不同,它确实使用了 delegate(除非事件没有选择器).Backbone 中的事件绑定如下所示:

It isn't similar to delegate it does use delegate (unless the event doesn't have a selector). The event binding in Backbone looks like this:

if (selector === '') {
  $(this.el).bind(eventName, method);
} else {
  $(this.el).delegate(selector, eventName, method);
}

所以它在视图的元素上使用 delegate .这至少将事件限制为视图中的元素.

So it uses delegate on the view's element. That at least limits the events to elements within the view.

您无法阻止人们在调试器中处理您的元素和事件.他们可以更改 HTML、CSS,甚至编辑您的 JavaScript,因此您无法阻止它们在您的页面上造成问题.不过,您可以阻止它们在您的服务器上造成混乱,只是不要相信 Backbone 发送到您服务器的任何内容,并像验证来自外部世界的任何其他内容一样验证所有内容.

You can't stop people from messing around with your elements and events in a debugger. They can change the HTML, the CSS, and even edit your JavaScript so you can't stop them from causing trouble on your page. You can stop them from making a mess on your server though, just don't trust anything that Backbone sends to your server and validate everything the same way you would validate anything else that comes in from the outside world.

基本上,不要浪费时间担心有人会因为弄乱您的 HTML/事件/JavaScript 而用砖头砸自己的脸.让他们随心所欲地伤害自己.但是,请不要相信来自外部世界的任何东西来保护您的服务器(您的服务器甚至不应该过度信任自己).

Basically, don't waste your time worrying about someone smashing their own face with a brick by messing with your HTML/events/JavaScript. Let them hurt themselves all they want. But do protect your server by not trusting anything from the outside world (and your servers shouldn't even trust themselves any more than they have to).

这篇关于Backbone.js 事件绑定.像“委托"一样在 Jquery 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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