1195
1195
}
1196
1196
} ) ;
1197
1197
1198
+ // TODO 暂时的替代方法
1198
1199
$ . fn . show = function ( ) {
1199
1200
this . css ( { display : 'block' } ) ;
1200
1201
}
1211
1212
});
1212
1213
*/
1213
1214
1215
+ var optionsCache = { } ;
1216
+ _ . createOptions = function ( options ) {
1217
+ var object = optionsCache [ options ] = { } ;
1218
+ $ . each ( options . match ( rnotwhite ) || [ ] , function ( i , flag ) {
1219
+ object [ flag ] = true ;
1220
+ } ) ;
1221
+ return object ;
1222
+ }
1223
+
1224
+ // 回调函数
1225
+ $ . Callbacks = function ( options ) {
1226
+ options = typeof options === 'string' ?
1227
+ ( optionsCache [ options ] || _ . createOptions ( options ) ) :
1228
+ $ . extend ( { } , options ) ;
1229
+
1230
+ var firing ,
1231
+ memory ,
1232
+ fired ,
1233
+ firingStart ,
1234
+ firingLength ,
1235
+ firingIndex ,
1236
+ list = [ ] , // 回调列表
1237
+ stack = ! options . once && [ ] ,
1238
+ fire = function ( data ) {
1239
+ memory = options . memory && data ; // 如果参数memory为true,则记录data
1240
+ fired = true ; // 标记触发回调
1241
+ firingIndex = firingStart || 0 ;
1242
+ firingStart = 0 ;
1243
+ firingLength = list . length ;
1244
+ firing = true ; // 标记正在触发回调
1245
+
1246
+ for ( ; list && firingIndex < firingLength ; firingIndex ++ ) {
1247
+ if ( list [ firingIndex ] . apply ( data [ 0 ] , data [ 1 ] ) === false && options . stopOnFalse ) {
1248
+ memory = false ; // 阻止未来可能由于add所产生的回调
1249
+ break ; // 由于参数stopOnFalse为true,所以当有回调函数返回值为false时退出循环
1250
+ }
1251
+ }
1252
+ firing = false ; // 标记回调结束
1253
+ if ( list ) {
1254
+ if ( stack ) {
1255
+ if ( stack . length ) {
1256
+ fire ( stack . shift ( ) ) ;
1257
+ }
1258
+ } else if ( memory ) { // 否则,如果有记忆
1259
+ list = [ ] ;
1260
+ } else { // 再否则阻止回调列表中的回调
1261
+ self . disable ( ) ;
1262
+ }
1263
+ }
1264
+ } ,
1265
+ self = {
1266
+ // 添加回调列表
1267
+ add : function ( ) {
1268
+ if ( list ) {
1269
+ var start = list . length ;
1270
+
1271
+ ( function add ( args ) {
1272
+ $ . each ( args , function ( i , arg ) {
1273
+ var type = $ . type ( args ) ;
1274
+
1275
+ if ( type === 'function' ) {
1276
+ if ( ! options . unique || ! self . has ( arg ) ) {
1277
+ list . push ( arg ) ;
1278
+ }
1279
+ } else if ( arg && arg . length && type !== 'string' ) { // 假如传过来的参数为数组或array-like,则继续调用添加,从这里可以看出add的传参可以有add(fn),add([fn1,fn2]),add(fn1,fn2)
1280
+ add ( arg ) ;
1281
+ }
1282
+ } ) ;
1283
+
1284
+ if ( firing ) {
1285
+ firingLength = list . length ;
1286
+ } else if ( memory ) {
1287
+ firingStart = start ;
1288
+ fire ( memory ) ;
1289
+ }
1290
+ } ( arguments ) ) ;
1291
+ }
1292
+ return this ;
1293
+ } ,
1294
+ remove : function ( ) {
1295
+ if ( list ) {
1296
+ $ . each ( arguments , function ( i , arg ) {
1297
+ var index ;
1298
+ while ( ( index = $ . inArray ( arg , list , index ) ) > - 1 ) {
1299
+ list . splice ( index , 1 ) ;
1300
+ if ( firing ) {
1301
+ if ( index < firingLength ) {
1302
+ firingLength -- ;
1303
+ }
1304
+ if ( index <= firingIndex ) {
1305
+ firingIndex -- ;
1306
+ }
1307
+ }
1308
+ }
1309
+ } ) ;
1310
+ }
1311
+ return this ;
1312
+ } ,
1313
+ has : function ( fn ) {
1314
+ return fn ? $ . inArray ( fn , list ) > - 1 : ! ! ( list && list . length ) ;
1315
+ } ,
1316
+ empty : function ( ) {
1317
+ list = [ ] ;
1318
+ firingLength = 0 ;
1319
+ return this ;
1320
+ } ,
1321
+ disable : function ( ) {
1322
+ list = stack = memory = undefined ;
1323
+ return this ;
1324
+ } ,
1325
+ disabled : function ( ) {
1326
+ return ! list ;
1327
+ } ,
1328
+ lock : function ( ) {
1329
+ stack = undefined ;
1330
+ if ( ! memory ) {
1331
+ self . disabled ( ) ;
1332
+ }
1333
+ return this ;
1334
+ } ,
1335
+ locked : function ( ) {
1336
+ return ! stack ;
1337
+ } ,
1338
+ fireWith : function ( context , args ) {
1339
+ if ( list && ( ! fired || stack ) ) {
1340
+ args = args || [ ] ;
1341
+ args = [ context , args . slice ? args . slice ( ) : args ] ;
1342
+
1343
+ if ( firing ) {
1344
+ stack . push ( args ) ;
1345
+ } else {
1346
+ fire ( args ) ;
1347
+ }
1348
+ }
1349
+ return this ;
1350
+ } ,
1351
+ fire : function ( ) {
1352
+ self . fireWith ( this , arguments ) ;
1353
+ return this ;
1354
+ } ,
1355
+ fired : function ( ) {
1356
+ return ! ! fired ;
1357
+ }
1358
+ } ;
1359
+
1360
+ return self ;
1361
+ }
1362
+
1363
+
1364
+
1214
1365
} ( window ) ) ;
1215
1366
1216
1367
// 2014-04-21 : 准备开发第一版
1222
1373
// 2014-05-08 : 增加$().css({ color : 'red' }), $(window).width,height(), $(document).width,height();
1223
1374
// 2014-05-09 : 增加$().width()方法, event first;
1224
1375
// 2014-05-12 : 增加$().data(), $().removeData()方法
1225
- // 2014-05-13 : 增加$().event.dispatch方法
1376
+ // 2014-05-13 : 增加$().event.dispatch方法
1377
+ // 2014-05-14 : 增加$().CallBacks()方法
0 commit comments