gpt4 book ai didi

javascript - Backbone.View.extend - 事件哈希

转载 作者:行者123 更新时间:2023-11-28 20:31:12 25 4
gpt4 key购买 nike

我正在阅读一个教程,该教程建议可以通过 View 捕获事件,如下所示:

var View = Backbone.View.extend({
el: $('#element'),
events: {},
render: function(){}
});

似乎没有任何与此事件对象相关的文档。它是否已被弃用或已过时?或者我只是找错地方了:http://backbonejs.org/

最佳答案

来自View.delegateEvents

delegateEvents delegateEvents([events])
Uses jQuery's on function to provide declarative callbacks for DOM events within a view. If an events hash is not passed directly, uses this.events as the source. Events are written in the format {"event selector": "callback"}. The callback may be either the name of a method on the view, or a direct function body. Omitting the selector causes the event to be bound to the view's root element (this.el). By default, delegateEvents is called within the View's constructor for you, so if you have a simple events hash, all of your DOM events will always already be connected, and you will never have to call this function yourself.

The events property may also be defined as a function that returns an events hash, to make it easier to programmatically define your events, as well as inherit them from parent views.

Using delegateEvents provides a number of advantages over manually using jQuery to bind events to child elements during render. All attached callbacks are bound to the view before being handed off to jQuery, so when the callbacks are invoked, this continues to refer to the view object. When delegateEvents is run again, perhaps with a different events hash, all callbacks are removed and delegated afresh — useful for views which need to behave differently when in different modes.

关于javascript - Backbone.View.extend - 事件哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16338420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com