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

Changeset 6626

Show
Ignore:
Timestamp:
04/29/07 05:37:07 (1 year ago)
Author:
sam
Message:

prototype: Add Function#curry, Function#delay, Function#defer, and Function#wrap. Closes #8134.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/trunk/CHANGELOG

    r6621 r6626  
    11*SVN* 
     2 
     3* Add Function#curry, Function#delay, Function#defer, and Function#wrap.  Closes #8134.  [Andrew Dupont, Tobie Langel, sam] 
    24 
    35*1.5.1* (it is a mystery) 
  • spinoffs/prototype/trunk/src/ajax.js

    r6558 r6626  
    108108        this.options.asynchronous); 
    109109 
    110       if (this.options.asynchronous) 
    111         setTimeout(function() { this.respondToReadyState(1) }.bind(this), 10); 
     110      if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1); 
    112111       
    113112      this.transport.onreadystatechange = this.onStateChange.bind(this); 
     
    268267     
    269268    if (this.success()) { 
    270       if (this.onComplete) 
    271         setTimeout(this.onComplete.bind(this), 10); 
     269      if (this.onComplete) this.onComplete.bind(this).defer(); 
    272270    } 
    273271  } 
     
    308306      this.lastText = request.responseText; 
    309307    } 
    310     this.timer = setTimeout(this.onTimerEvent.bind(this),  
    311       this.decay * this.frequency * 1000); 
     308    this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency); 
    312309  }, 
    313310 
  • spinoffs/prototype/trunk/src/array.js

    r6555 r6626  
    1 var $A = Array.from = function(iterable) { 
     1function $A(iterable) { 
    22  if (!iterable) return []; 
    33  if (iterable.toArray) { 
     
    1212 
    1313if (Prototype.Browser.WebKit) { 
    14   $A = Array.from = function(iterable) { 
     14  function $A(iterable) { 
    1515    if (!iterable) return []; 
    1616    if (!(typeof iterable == 'function' && iterable == '[object NodeList]') &&  
     
    2525  } 
    2626} 
     27 
     28Array.from = $A; 
    2729 
    2830Object.extend(Array.prototype, Enumerable); 
  • spinoffs/prototype/trunk/src/base.js

    r6555 r6626  
    6767}); 
    6868 
    69 Function.prototype.bind = function() { 
    70   var __method = this, args = $A(arguments), object = args.shift(); 
    71   return function() { 
    72     return __method.apply(object, args.concat($A(arguments))); 
     69Object.extend(Function.prototype, { 
     70  bind: function() { 
     71    var __method = this, args = $A(arguments), object = args.shift(); 
     72    return function() { 
     73      return __method.apply(object, args.concat($A(arguments))); 
     74    } 
     75  }, 
     76   
     77  bindAsEventListener: function() { 
     78    var __method = this, args = $A(arguments), object = args.shift(); 
     79    return function(event) { 
     80      return __method.apply(object, [(event || window.event)].concat(args).concat($A(arguments))); 
     81    } 
     82  }, 
     83   
     84  curry: function() { 
     85    var __method = this, args = $A(arguments); 
     86    return function() { 
     87      return __method.apply(this, args.concat($A(arguments))); 
     88    } 
     89  }, 
     90 
     91  delay: function() {  
     92    var __method = this, args = $A(arguments), timeout = args.shift() * 1000;  
     93    return window.setTimeout(function() { 
     94      return __method.apply(__method, args); 
     95    }, timeout); 
     96  }, 
     97   
     98  wrap: function(wrapper) { 
     99    var __method = this; 
     100    return function() { 
     101      return wrapper.apply(this, [__method.bind(this)].concat($A(arguments)));  
     102    } 
    73103  } 
    74 } 
     104}); 
    75105 
    76 Function.prototype.bindAsEventListener = function(object) { 
    77   var __method = this, args = $A(arguments), object = args.shift(); 
    78   return function(event) { 
    79     return __method.apply(object, [( event || window.event)].concat(args).concat($A(arguments))); 
    80   } 
    81 
     106Function.prototype.defer = Function.prototype.delay.curry(0.01); 
    82107 
    83108Object.extend(Number.prototype, { 
  • spinoffs/prototype/trunk/src/dom.js

    r6598 r6626  
    106106    html = typeof html == 'undefined' ? '' : html.toString(); 
    107107    $(element).innerHTML = html.stripScripts(); 
    108     setTimeout(function() {html.evalScripts()}, 10); 
     108    html.evalScripts.bind(html).defer(); 
    109109    return element; 
    110110  }, 
     
    121121        range.createContextualFragment(html.stripScripts()), element); 
    122122    } 
    123     setTimeout(function() {html.evalScripts()}, 10); 
     123    html.evalScripts.bind(html).defer(); 
    124124    return element; 
    125125  }, 
     
    505505      element.innerHTML = html.stripScripts(); 
    506506    } 
    507     setTimeout(function() { html.evalScripts() }, 10); 
     507    html.evalScripts.bind(html).defer(); 
    508508    return element; 
    509509  } 
     
    699699    } 
    700700 
    701     setTimeout(function() {content.evalScripts()}, 10);    
     701    content.evalScripts.bind(content).defer();    
    702702  }, 
    703703   
  • spinoffs/prototype/trunk/test/unit/base.html

    r6555 r6626  
    8686    }}, 
    8787     
     88    testFunctionCurry: function() { with(this) { 
     89      var split = function(delimiter, string) { return string.split(delimiter); }; 
     90      var splitOnColons = split.curry(":"); 
     91      assertEnumEqual(split(":", "0:1:2:3:4:5"), splitOnColons("0:1:2:3:4:5")); 
     92    }}, 
     93     
     94    testFunctionDelay: function() { with(this) { 
     95      window.delayed = undefined; 
     96      var delayedFunction = function() { window.delayed = true; }; 
     97      var delayedFunctionWithArgs = function() { window.delayedWithArgs = $A(arguments).join(' '); }; 
     98      delayedFunction.delay(0.8); 
     99      delayedFunctionWithArgs.delay(0.8, 'hello', 'world'); 
     100      assertUndefined(window.delayed); 
     101      wait(1000, function() { 
     102        assert(window.delayed); 
     103        assertEqual('hello world', window.delayedWithArgs); 
     104      }); 
     105    }}, 
     106     
     107    testFunctionWrap: function() { with(this) { 
     108      function sayHello(){ 
     109        return 'hello world'; 
     110      }; 
     111       
     112      assertEqual('HELLO WORLD', sayHello.wrap(function(proceed) { 
     113        return proceed().toUpperCase(); 
     114      })()); 
     115       
     116      var temp = String.prototype.capitalize; 
     117      String.prototype.capitalize = String.prototype.capitalize.wrap(function(proceed, eachWord) { 
     118        if(eachWord && this.include(' ')) return this.split(' ').map(function(str){ 
     119          return str.capitalize(); 
     120        }).join(' '); 
     121        return proceed(); 
     122      }); 
     123      assertEqual('Hello world', 'hello world'.capitalize()); 
     124      assertEqual('Hello World', 'hello world'.capitalize(true)); 
     125      assertEqual('Hello', 'hello'.capitalize()); 
     126      String.prototype.capitalize = temp; 
     127    }}, 
     128     
     129    testFunctionDefer: function() { with(this) { 
     130      window.deferred = undefined; 
     131      var deferredFunction = function() { window.deferred = true; }; 
     132      deferredFunction.defer(); 
     133      assertUndefined(window.deferred); 
     134      wait(50, function() { 
     135        assert(window.deferred); 
     136      }); 
     137    }}, 
     138 
    88139    testObjectInspect: function() { with(this) { 
    89140      assertEqual('undefined', Object.inspect());