Permalink
Browse files

bind now prepends arguments

  • Loading branch information...
1 parent 75de8bf commit 190900dd9f187434708a16d06838362f29124858 @ryanflorence committed May 12, 2011
Showing with 17 additions and 5 deletions.
  1. +4 −3 docs/index.html
  2. +3 −2 src/core.js
  3. +10 −0 tests/tests.js
View
@@ -258,7 +258,7 @@ <h2 id=snack.bind><a href="#snack.bind">snack.bind</a></h2>
<ol>
<li><code>fn</code> (<b>function</b>) - The function to bind the context to.</li>
<li><code>context</code> (<b>object</b>) - The object to become the context of the function.</li>
- <li><code>args</code> (<b>array</b>: optional) - An array of overriding arguments to apply to the function.</li>
+ <li><code>args</code> (<b>array</b>: optional) - An array of arguments to prepend to the existing arguments of the function.</li>
</ol>
<h3>Returns</h3>
@@ -272,12 +272,13 @@ <h2 id=snack.bind><a href="#snack.bind">snack.bind</a></h2>
init: function (){
this.listener = snack.listener(
{node: this.node},
- snack.bind(this.handler, this) // here!
+ snack.bind(this.handler, this, ['foo', 'bar']) // here!
)
},
- handler: function (){
+ handler: function (foo, bar, event){
// `this` is `widget`, instead of `node` (default context)
+ // foo and bar are added before event
}
}
</code></pre>
View
@@ -22,7 +22,8 @@ if (typeof Object.create != 'function'){
var snack = window.snack = {}
, guid = 0
, toString = Object.prototype.toString
- , indexOf = Array.prototype.indexOf
+ , indexOf = [].indexOf
+ , push = [].push
snack.extend = function (){
if (arguments.length == 1)
@@ -43,7 +44,7 @@ if (typeof Object.create != 'function'){
bind: function (fn, context, args) {
args = args || [];
return function (){
- args.push.apply(args, arguments);
+ push.apply(args, arguments);
return fn.apply(context, args)
}
},
View
@@ -107,6 +107,16 @@ test('bind should', function (){
var bound = snack.bind(add, obj, [3])
equal(bound(), 5, 'apply new arguments if defined')
+
+ function toBind() {
+ return 'args: ' + [].slice.call(arguments)
+ }
+ var bound = snack.bind(toBind, {}, ['a', 'b', 'c']);
+
+ equal(bound(), 'args: a,b,c')
+ bound(); // "args: a,b,c" - same in both versions
+ bound('d'); // "args: a,b,c,d" - would previously return "args: a,b,c"
+
})

0 comments on commit 190900d

Please sign in to comment.