comparison default/node_modules/shoestring/src/events/unbind.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 //>>excludeStart("exclude", pragmas.exclude);
2 define([ "shoestring" ], function(){
3 //>>excludeEnd("exclude");
4
5 /**
6 * Unbind a previous bound callback for an event.
7 *
8 * @param {string} event The event(s) the callback was bound to..
9 * @param {function} callback Callback to unbind.
10 * @return shoestring
11 * @this shoestring
12 */
13 shoestring.fn.unbind = function( event, callback ){
14
15 //>>includeStart("development", pragmas.development);
16 if( arguments.length >= 3 || typeof callback === "string" ){
17 shoestring.error( 'off-delegate' );
18 }
19 //>>includeEnd("development");
20
21 var evts = event ? event.split( " " ) : [];
22
23 return this.each(function(){
24 if( !this.shoestringData || !this.shoestringData.events ) {
25 return;
26 }
27
28 if( !evts.length ) {
29 unbindAll.call( this );
30 } else {
31 var split, evt, namespace;
32 for( var i = 0, il = evts.length; i < il; i++ ){
33 split = evts[ i ].split( "." ),
34 evt = split[ 0 ],
35 namespace = split.length > 0 ? split[ 1 ] : null;
36
37 if( evt ) {
38 unbind.call( this, evt, namespace, callback );
39 } else {
40 unbindAll.call( this, namespace, callback );
41 }
42 }
43 }
44 });
45 };
46
47 function unbind( evt, namespace, callback ) {
48 var bound = this.shoestringData.events[ evt ];
49 if( !(bound && bound.length) ) {
50 return;
51 }
52
53 var matched = [], j, jl;
54 for( j = 0, jl = bound.length; j < jl; j++ ) {
55 if( !namespace || namespace === bound[ j ].namespace ) {
56 if( callback === undefined || callback === bound[ j ].originalCallback ) {
57 this.removeEventListener( evt, bound[ j ].callback, false );
58 matched.push( j );
59 }
60 }
61 }
62
63 for( j = 0, jl = matched.length; j < jl; j++ ) {
64 this.shoestringData.events[ evt ].splice( j, 1 );
65 }
66 }
67
68 function unbindAll( namespace, callback ) {
69 for( var evtKey in this.shoestringData.events ) {
70 unbind.call( this, evtKey, namespace, callback );
71 }
72 }
73
74 shoestring.fn.off = shoestring.fn.unbind;
75 //>>excludeStart("exclude", pragmas.exclude);
76 });
77 //>>excludeEnd("exclude");