gjs r125 - in trunk: modules test/js



Author: johan
Date: Mon Dec  1 19:12:20 2008
New Revision: 125
URL: http://svn.gnome.org/viewvc/gjs?rev=125&view=rev

Log:
Bug 561849 â Alternate way of connecting signals.

Add a Lang.bind function which binds the meaning of this
within a context.



Modified:
   trunk/modules/lang.js
   trunk/test/js/testLang.js

Modified: trunk/modules/lang.js
==============================================================================
--- trunk/modules/lang.js	(original)
+++ trunk/modules/lang.js	Mon Dec  1 19:12:20 2008
@@ -78,3 +78,35 @@
             removeNullProperties(obj[property]);
     }
 }
+
+/**
+ * Binds obj to callback. Makes it possible to refer to "obj"
+ * using this within the callback.
+ * @param {object} obj the object to bind
+ * @param {function} callback callback to bind obj in
+ * @param arguments additional arguments to the callback
+ * @returns: a new callback
+ * @type: function
+ */
+function bind(obj, callback) {
+    let me = obj;
+    let bindArguments = Array.prototype.slice.call(arguments, 2);
+
+    if (typeof(obj) != 'object') {
+        throw new Error(
+            "first argument to Lang.bind() must be an object, not " +
+                typeof(obj));
+    }
+
+    if (typeof(callback) != 'function') {
+        throw new Error(
+            "second argument to Lang.bind() must be a function, not " +
+                typeof(callback));
+    }
+
+    return function() {
+        let args = Array.prototype.slice.call(arguments);
+        args = args.concat(bindArguments);
+        return callback.apply(me, args);
+    };
+}

Modified: trunk/test/js/testLang.js
==============================================================================
--- trunk/test/js/testLang.js	(original)
+++ trunk/test/js/testLang.js	Mon Dec  1 19:12:20 2008
@@ -63,4 +63,48 @@
     assertTrue("bar 'a' new value is 13", (bar.a == 13));
 }
 
+function testBind() {
+
+    function Obj() {
+    }
+
+    Obj.prototype = {
+        callback: function() {
+            this.obj = this;
+            this.args = arguments;
+            return true;
+        }
+    };
+
+    let callback;
+
+    let o = new Obj();
+    callback = Lang.bind(o, o.callback);
+    assertEquals(callback(), true);
+    assertNotEquals("o.obj in callback", undefined, o.obj);
+    assertEquals("o.obj in callback", o, o.obj);
+    assertEquals("o.args in callback", 0, o.args.length);
+    assertRaises(function() { return Lang.bind(o, undefined); });
+    assertRaises(function() { return Lang.bind(undefined, function() {}); });
+
+    let o2 = new Obj();
+    callback = Lang.bind(o2, o2.callback, 42, 1138);
+    assertEquals(callback(), true);
+    assertNotEquals("o2.args in callback", undefined, o2.args);
+    assertEquals("o2.args.length in callback", 2, o2.args.length);
+    assertEquals("o2.args[0] in callback", 42, o2.args[0]);
+    assertEquals("o2.args[1] in callback", 1138, o2.args[1]);
+
+    let o3 = new Obj();
+    callback = Lang.bind(o3, o3.callback, 42, 1138);
+    assertEquals(callback(1, 2, 3), true);
+    assertNotEquals("o3.args in callback", undefined, o3.args);
+    assertEquals("o3.args.length in callback", 5, o3.args.length);
+    assertEquals("o3.args[0] in callback", 1, o3.args[0]);
+    assertEquals("o3.args[1] in callback", 2, o3.args[1]);
+    assertEquals("o3.args[2] in callback", 3, o3.args[2]);
+    assertEquals("o3.args[3] in callback", 42, o3.args[3]);
+    assertEquals("o3.args[4] in callback", 1138, o3.args[4]);
+}
+
 gjstestRun();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]