Commit 4965eab4 authored by Romain Courteaud's avatar Romain Courteaud

Add declareAcquiredMethod.

Replace the gadget method "acquire" by the Klass function
"declareAcquiredMethod" which generates new accessors on this class and do a
direct mapping with the parent aq_dynamic function.
parent 52a76f99
...@@ -84,26 +84,41 @@ ...@@ -84,26 +84,41 @@
throw new Error("No element defined"); throw new Error("No element defined");
} }
return this.__element; return this.__element;
})
.declareMethod('acquire', function () {
var gadget = this,
argument_list = arguments;
return new RSVP.Queue()
.push(function () {
var aq_dynamic = gadget.aq_dynamic;
if (aq_dynamic !== undefined) {
return aq_dynamic.apply(gadget, argument_list);
}
throw new renderJS.AcquisitionError("aq_dynamic is not defined");
})
.push(undefined, function (error) {
if (error instanceof renderJS.AcquisitionError) {
return gadget.aq_parent.apply(gadget, argument_list);
}
throw error;
});
}); });
/////////////////////////////////////////////////////////////////
// RenderJSGadget.declareAcquiredMethod
/////////////////////////////////////////////////////////////////
function acquire() {
var gadget = this,
argument_list = arguments;
return new RSVP.Queue()
.push(function () {
var aq_dynamic = gadget.aq_dynamic;
if (aq_dynamic !== undefined) {
return aq_dynamic.apply(gadget, argument_list);
}
throw new renderJS.AcquisitionError("aq_dynamic is not defined");
})
.push(undefined, function (error) {
if (error instanceof renderJS.AcquisitionError) {
return gadget.aq_parent.apply(gadget, argument_list);
}
throw error;
});
}
RenderJSGadget.declareAcquiredMethod =
function (name, method_name_to_acquire) {
this.prototype[name] = function () {
return acquire.apply(this, [method_name_to_acquire,
Array.prototype.slice.call(arguments, 0)]);
};
// Allow chain
return this;
};
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSEmbeddedGadget // RenderJSEmbeddedGadget
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -270,7 +285,7 @@ ...@@ -270,7 +285,7 @@
return "OK"; return "OK";
}); });
gadget_instance.__chan.bind("acquire", function (trans, params) { gadget_instance.__chan.bind("acquire", function (trans, params) {
gadget_instance.acquire.apply(gadget_instance, params) acquire.apply(gadget_instance, params)
.then(function (g) { .then(function (g) {
trans.complete(g); trans.complete(g);
}).fail(function (e) { }).fail(function (e) {
...@@ -330,7 +345,7 @@ ...@@ -330,7 +345,7 @@
var i; var i;
// Define aq_parent to reach parent gadget // Define aq_parent to reach parent gadget
gadget_instance.aq_parent = function (method_name, argument_list) { gadget_instance.aq_parent = function (method_name, argument_list) {
return parent_gadget.acquire(method_name, argument_list); return acquire.apply(parent_gadget, [method_name, argument_list]);
}; };
// Drop the current loading klass info used by selector // Drop the current loading klass info used by selector
gadget_loading_klass = undefined; gadget_loading_klass = undefined;
...@@ -483,6 +498,8 @@ ...@@ -483,6 +498,8 @@
tmp_constructor.__ready_list = RenderJSGadget.__ready_list.slice(); tmp_constructor.__ready_list = RenderJSGadget.__ready_list.slice();
tmp_constructor.declareMethod = tmp_constructor.declareMethod =
RenderJSGadget.declareMethod; RenderJSGadget.declareMethod;
tmp_constructor.declareAcquiredMethod =
RenderJSGadget.declareAcquiredMethod;
tmp_constructor.ready = tmp_constructor.ready =
RenderJSGadget.ready; RenderJSGadget.ready;
tmp_constructor.prototype = new RenderJSGadget(); tmp_constructor.prototype = new RenderJSGadget();
...@@ -635,6 +652,8 @@ ...@@ -635,6 +652,8 @@
RenderJSGadget.call(this); RenderJSGadget.call(this);
}; };
tmp_constructor.declareMethod = RenderJSGadget.declareMethod; tmp_constructor.declareMethod = RenderJSGadget.declareMethod;
tmp_constructor.declareAcquiredMethod =
RenderJSGadget.declareAcquiredMethod;
tmp_constructor.__ready_list = RenderJSGadget.__ready_list.slice(); tmp_constructor.__ready_list = RenderJSGadget.__ready_list.slice();
tmp_constructor.ready = RenderJSGadget.ready; tmp_constructor.ready = RenderJSGadget.ready;
tmp_constructor.prototype = new RenderJSGadget(); tmp_constructor.prototype = new RenderJSGadget();
...@@ -713,6 +732,9 @@ ...@@ -713,6 +732,9 @@
return result; return result;
}; };
tmp_constructor.declareAcquiredMethod =
RenderJSGadget.declareAcquiredMethod;
// Define aq_parent to inform parent window // Define aq_parent to inform parent window
tmp_constructor.prototype.aq_parent = function (method_name, tmp_constructor.prototype.aq_parent = function (method_name,
argument_list) { argument_list) {
......
...@@ -24,8 +24,14 @@ ...@@ -24,8 +24,14 @@
.declareMethod('getContent', function () { .declareMethod('getContent', function () {
return this.embedded_property; return this.embedded_property;
}) })
.declareMethod('callAcquire', function (method_name, param_list) { .declareAcquiredMethod('plugOKAcquire', 'acquireMethodRequested')
return this.acquire(method_name, param_list); .declareMethod('callOKAcquire', function (param1, param2) {
return this.plugOKAcquire(param1, param2);
})
.declareAcquiredMethod('plugErrorAcquire',
'acquireMethodRequestedWithAcquisitionError')
.declareMethod('callErrorAcquire', function (param1, param2) {
return this.plugErrorAcquire(param1, param2);
}); });
}(window, rJS)); }(window, rJS));
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global console */
(function (document, renderJS, QUnit, sinon) { (function (document, renderJS, QUnit, sinon) {
"use strict"; "use strict";
var test = QUnit.test, var test = QUnit.test,
...@@ -1135,32 +1136,83 @@ ...@@ -1135,32 +1136,83 @@
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSGadget.acquire // RenderJSGadgetKlass.declareAcquiredMethod
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("RenderJSGadget.acquire", { module("RenderJSGadgetKlass.declareAcquiredMethod", {
setup: function () { setup: function () {
renderJS.clearGadgetKlassList(); renderJS.clearGadgetKlassList();
} }
}); });
test('is chainable', function () {
// Check that declareAcquiredMethod is chainable
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget, result;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareAcquiredMethod = RenderJSGadget.declareAcquiredMethod;
gadget = new Klass();
equal(gadget.testFoo, undefined);
result = Klass.declareAcquiredMethod('testFoo', 'testBar');
// declareAcquiredMethod is chainable
equal(result, Klass);
});
test('creates methods on the prototype', function () {
// Check that declareAcquiredMethod create a callable on the prototype
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareAcquiredMethod = RenderJSGadget.declareAcquiredMethod;
gadget = new Klass();
equal(gadget.testFoo, undefined);
Klass.declareAcquiredMethod('testFoo', 'testBar');
// Method is added on the instance class prototype
equal(RenderJSGadget.prototype.testFoo, undefined);
ok(gadget.testFoo !== undefined);
ok(Klass.prototype.testFoo !== undefined);
equal(Klass.prototype.testFoo, gadget.testFoo);
});
test('returns aq_dynamic result if available', function () { test('returns aq_dynamic result if available', function () {
var gadget = new RenderJSGadget(), // Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget,
aq_dynamic_called = false, aq_dynamic_called = false,
original_method_name = "foo", original_method_name = "foo",
original_argument_list = ["foobar", "barfoo"]; original_argument_list = ["foobar", "barfoo"];
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareAcquiredMethod = RenderJSGadget.declareAcquiredMethod;
Klass.declareAcquiredMethod("checkIfAqDynamicIsCalled",
original_method_name);
gadget = new Klass();
gadget.aq_dynamic = function (method_name, argument_list) { gadget.aq_dynamic = function (method_name, argument_list) {
aq_dynamic_called = true; aq_dynamic_called = true;
equal(this, gadget, "Context should be kept"); equal(this, gadget, "Context should be kept");
equal(method_name, original_method_name, "Method name should be kept"); equal(method_name, original_method_name, "Method name should be kept");
equal(argument_list, original_argument_list, deepEqual(argument_list, original_argument_list,
"Argument list should be kept" "Argument list should be kept"
); );
return "FOO"; return "FOO";
}; };
stop(); stop();
gadget.acquire(original_method_name, original_argument_list) gadget.checkIfAqDynamicIsCalled("foobar", "barfoo")
.then(function (result) { .then(function (result) {
equal(result, "FOO"); equal(result, "FOO");
equal(aq_dynamic_called, true); equal(aq_dynamic_called, true);
...@@ -1171,15 +1223,26 @@ ...@@ -1171,15 +1223,26 @@
}); });
test('fails if aq_dynamic throws an error', function () { test('fails if aq_dynamic throws an error', function () {
var gadget = new RenderJSGadget(), // Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget,
original_error = new Error("Custom error for the test"); original_error = new Error("Custom error for the test");
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareAcquiredMethod = RenderJSGadget.declareAcquiredMethod;
Klass.declareAcquiredMethod("checkIfAqDynamicThrowsError",
"foo");
gadget = new Klass();
gadget.aq_dynamic = function () { gadget.aq_dynamic = function () {
throw original_error; throw original_error;
}; };
stop(); stop();
gadget.acquire() gadget.checkIfAqDynamicThrowsError()
.fail(function (error) { .fail(function (error) {
equal(error, original_error); equal(error, original_error);
equal(error.message, "Custom error for the test"); equal(error.message, "Custom error for the test");
...@@ -1191,12 +1254,23 @@ ...@@ -1191,12 +1254,23 @@
test('returns aq_parent result if aq_dynamic raises AcquisitionError', test('returns aq_parent result if aq_dynamic raises AcquisitionError',
function () { function () {
var gadget = new RenderJSGadget(), // Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget,
i = 0, i = 0,
aq_dynamic_called = false, aq_dynamic_called = false,
aq_parent_called = false, aq_parent_called = false,
original_method_name = "foo", original_method_name = "foo",
original_argument_list = ["foobar", "barfoo"]; original_argument_list = ["foobar", "barfoo"];
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareAcquiredMethod = RenderJSGadget.declareAcquiredMethod;
Klass.declareAcquiredMethod("checkIfAqDynamicThrowsAcqError",
original_method_name);
gadget = new Klass();
gadget.aq_dynamic = function (method_name, argument_list) { gadget.aq_dynamic = function (method_name, argument_list) {
aq_dynamic_called = true; aq_dynamic_called = true;
...@@ -1210,14 +1284,14 @@ ...@@ -1210,14 +1284,14 @@
equal(i, 1, "aq_parent called after aq_dynamic"); equal(i, 1, "aq_parent called after aq_dynamic");
equal(this, gadget, "Context should be kept"); equal(this, gadget, "Context should be kept");
equal(method_name, original_method_name, "Method name should be kept"); equal(method_name, original_method_name, "Method name should be kept");
equal(argument_list, original_argument_list, deepEqual(argument_list, original_argument_list,
"Argument list should be kept" "Argument list should be kept"
); );
return "FOO"; return "FOO";
}; };
stop(); stop();
gadget.acquire(original_method_name, original_argument_list) gadget.checkIfAqDynamicThrowsAcqError("foobar", "barfoo")
.then(function (result) { .then(function (result) {
equal(result, "FOO"); equal(result, "FOO");
equal(aq_dynamic_called, true); equal(aq_dynamic_called, true);
...@@ -1230,23 +1304,35 @@ ...@@ -1230,23 +1304,35 @@
test('returns aq_parent result if aq_dynamic does not exists', test('returns aq_parent result if aq_dynamic does not exists',
function () { function () {
var gadget = new RenderJSGadget(), // Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget,
aq_parent_called = false, aq_parent_called = false,
original_method_name = "foo", original_method_name = "foo",
original_argument_list = ["foobar", "barfoo"]; original_argument_list = ["foobar", "barfoo"];
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareAcquiredMethod = RenderJSGadget.declareAcquiredMethod;
Klass.declareAcquiredMethod("checkIfAqDynamicIsUndefined",
original_method_name);
gadget = new Klass();
gadget.aq_parent = function (method_name, argument_list) { gadget.aq_parent = function (method_name, argument_list) {
aq_parent_called = true; aq_parent_called = true;
equal(this, gadget, "Context should be kept"); equal(this, gadget, "Context should be kept");
equal(method_name, original_method_name, "Method name should be kept"); equal(method_name, original_method_name, "Method name should be kept");
equal(argument_list, original_argument_list, deepEqual(argument_list, original_argument_list,
"Argument list should be kept" "Argument list should be kept"
); );
return "FOO"; return "FOO";
}; };
stop(); stop();
gadget.acquire(original_method_name, original_argument_list) gadget.checkIfAqDynamicIsUndefined("foobar", "barfoo")
.then(function (result) { .then(function (result) {
equal(result, "FOO"); equal(result, "FOO");
equal(aq_parent_called, true); equal(aq_parent_called, true);
...@@ -1257,15 +1343,26 @@ ...@@ -1257,15 +1343,26 @@
}); });
test('fails if aq_parent throws an error', function () { test('fails if aq_parent throws an error', function () {
var gadget = new RenderJSGadget(), // Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget,
original_error = new Error("Custom error for the test"); original_error = new Error("Custom error for the test");
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareAcquiredMethod = RenderJSGadget.declareAcquiredMethod;
Klass.declareAcquiredMethod("checkIfAqParentThrowsError",
"foo");
gadget = new Klass();
gadget.aq_parent = function () { gadget.aq_parent = function () {
throw original_error; throw original_error;
}; };
stop(); stop();
gadget.acquire() gadget.checkIfAqParentThrowsError()
.fail(function (error) { .fail(function (error) {
equal(error, original_error); equal(error, original_error);
equal(error.message, "Custom error for the test"); equal(error.message, "Custom error for the test");
...@@ -1276,11 +1373,21 @@ ...@@ -1276,11 +1373,21 @@
}); });
test('fails if aq_parent is not defined', function () { test('fails if aq_parent is not defined', function () {
// Gadget klass should break the acquisition chain // Subclass RenderJSGadget to not pollute its namespace
var gadget = new RenderJSGadget(); var Klass = function () {
RenderJSGadget.call(this);
}, gadget;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareAcquiredMethod = RenderJSGadget.declareAcquiredMethod;
Klass.declareAcquiredMethod("checkIfAqParentIsUndefined",
"foo");
gadget = new Klass();
stop(); stop();
gadget.acquire() gadget.checkIfAqParentIsUndefined()
.fail(function (error) { .fail(function (error) {
ok(error instanceof TypeError); ok(error instanceof TypeError);
ok((error.message === "gadget.aq_parent is undefined") || ok((error.message === "gadget.aq_parent is undefined") ||
...@@ -2076,7 +2183,7 @@ ...@@ -2076,7 +2183,7 @@
original_argument_list = ["foobar", "barfoo"], original_argument_list = ["foobar", "barfoo"],
html_url = 'http://example.org/files/qunittest/test353.html'; html_url = 'http://example.org/files/qunittest/test353.html';
gadget.acquire = function (method_name, argument_list) { gadget.aq_parent = function (method_name, argument_list) {
acquire_called = true; acquire_called = true;
equal(this, gadget, "Context should be kept"); equal(this, gadget, "Context should be kept");
equal(method_name, original_method_name, "Method name should be kept"); equal(method_name, original_method_name, "Method name should be kept");
...@@ -2093,7 +2200,7 @@ ...@@ -2093,7 +2200,7 @@
stop(); stop();
gadget.declareGadget(html_url) gadget.declareGadget(html_url)
.then(function (new_gadget) { .then(function (new_gadget) {
return new_gadget.acquire( return new_gadget.aq_parent(
original_method_name, original_method_name,
original_argument_list original_argument_list
); );
...@@ -2253,7 +2360,7 @@ ...@@ -2253,7 +2360,7 @@
acquire_called = false, acquire_called = false,
url = "./embedded.html"; url = "./embedded.html";
gadget.acquire = function (method_name, argument_list) { gadget.aq_parent = function (method_name, argument_list) {
acquire_called = true; acquire_called = true;
equal(this, gadget, "Context should be kept"); equal(this, gadget, "Context should be kept");
if (method_name === "acquireMethodRequested") { if (method_name === "acquireMethodRequested") {
...@@ -2315,10 +2422,7 @@ ...@@ -2315,10 +2422,7 @@
// acquire check correctly returns result // acquire check correctly returns result
.push(function () { .push(function () {
return new_gadget.callAcquire( return new_gadget.callOKAcquire("param1", "param2");
"acquireMethodRequested",
["param1", "param2"]
);
}) })
.push(function (result) { .push(function (result) {
ok(acquire_called); ok(acquire_called);
...@@ -2327,7 +2431,7 @@ ...@@ -2327,7 +2431,7 @@
// acquire correctly returns error // acquire correctly returns error
.push(function () { .push(function () {
return new_gadget.callAcquire( return new_gadget.callErrorAcquire(
"acquireMethodRequestedWithAcquisitionError", "acquireMethodRequestedWithAcquisitionError",
["param1", "param2"] ["param1", "param2"]
); );
...@@ -2339,7 +2443,8 @@ ...@@ -2339,7 +2443,8 @@
equal( equal(
error, error,
"AcquisitionError: Can not handle " + "AcquisitionError: Can not handle " +
"acquireMethodRequestedWithAcquisitionError" "acquireMethodRequestedWithAcquisitionError",
error
); );
}); });
}) })
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment