Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7061

Show
Ignore:
Timestamp:
06/19/07 14:45:19 (1 year ago)
Author:
mislav
Message:

Prototype: refactor applyToCollection so it handles not only collections of observers, but event types also.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/branches/event/src/event.js

    r7056 r7061  
    230230  } 
    231231 
    232   // applies the calling method to an array of elements 
    233   var applyToCollection = function(args) { 
    234     if (args[0].constructor != Array) return false; 
    235     var method = args.callee; 
    236     args = $A(args); 
    237     args.shift().each(function(el){ method.apply(null, [el].concat(args)) }); 
    238     return true; // the calling method can end 
    239   }; 
    240  
    241232  // onReady helper stuff 
    242233  var readyCallbacks, timer, domReady = function() { 
     
    249240  }; 
    250241   
     242  // applies the calling method to an array of elements 
     243  var applyToCollection = function(args) { 
     244    switch ([0, 1].detect(function(i){ return args[i] && args[i].constructor == Array })) { 
     245    case 0: 
     246      // multiple observers 
     247      var method = args.callee; 
     248      args = $A(args); 
     249      args.shift().each(function(element) { 
     250        method.apply(null, [element].concat(args)); 
     251      }); 
     252      return true; // the calling method can end 
     253    case 1: 
     254      // multiple types 
     255      args[1].each(function(type) { 
     256        args.callee.call(null, args[0], type, args[2], args[3]) 
     257      }); 
     258      return true; 
     259    default: 
     260      return false; 
     261    } 
     262  }; 
     263 
    251264  return { 
    252265    observe: function(element, type, callback, useCapture) {