官术网_书友最值得收藏!

Using custom event namespacing

As a closing note for this chapter, we will present, in short, the mechanism that jQuery provides for namespacing custom events. The main benefit of event namespacing is that it allows us to use more specific event names that better describe their purpose, while also helping us to avoid conflicts between different implementation parts and plugins. It also provides a convenient way to unbind all the events of a given namespace from any target (element or broker).

A simple example implementation will look as follows:

var broker = $({});
broker.on('close.dialog', function (event, message){
    console.log(event.type, event.namespace);
});
broker.trigger('close.dialog', ['messageEmitted']);
broker.off('.dialog');
// removes all event handlers of the "dialog" namespace

For more information, you can visit the documentation page at http://docs.jquery.com/Namespaced_Events and the article at https://css-tricks.com/namespaced-events-jquery/ from the CSS-Tricks website.

主站蜘蛛池模板: 和田县| 巴塘县| 福鼎市| 六安市| 长治市| 普安县| 抚松县| 安顺市| 肃宁县| 屏山县| 汝南县| 江山市| 潼关县| 芜湖市| 万山特区| 紫金县| 德阳市| 连山| 莱阳市| 两当县| 左权县| 洪湖市| 清镇市| 平安县| 和林格尔县| 甘洛县| 宝兴县| 南江县| 长沙县| 清镇市| 怀化市| 南靖县| 安阳县| 永福县| 昌吉市| 甘谷县| 万州区| 鹤峰县| 池州市| 格尔木市| 印江|