Commit 2cdfe26b authored by Romain Courteaud's avatar Romain Courteaud

Do not prevent calling declareGadget in ready functions.

parent 4255bd41
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
javascript_registration_dict = {}, javascript_registration_dict = {},
stylesheet_registration_dict = {}, stylesheet_registration_dict = {},
gadget_loading_klass, gadget_loading_klass,
loading_gadget_promise, loading_klass_promise,
renderJS; renderJS;
function removeHash(url) { function removeHash(url) {
...@@ -353,7 +353,8 @@ ...@@ -353,7 +353,8 @@
.declareMethod('declareGadget', function (url, options) { .declareMethod('declareGadget', function (url, options) {
var queue, var queue,
parent_gadget = this, parent_gadget = this,
previous_loading_gadget_promise = loading_gadget_promise; local_loading_klass_promise,
previous_loading_klass_promise = loading_klass_promise;
if (options === undefined) { if (options === undefined) {
options = {}; options = {};
...@@ -365,10 +366,10 @@ ...@@ -365,10 +366,10 @@
// transform url to absolute url if it is relative // transform url to absolute url if it is relative
url = renderJS.getAbsoluteURL(url, this.__path); url = renderJS.getAbsoluteURL(url, this.__path);
// Change the global variable to update the loading queue // Change the global variable to update the loading queue
queue = new RSVP.Queue() loading_klass_promise = new RSVP.Queue()
// Wait for previous gadget loading to finish first // Wait for previous gadget loading to finish first
.push(function () { .push(function () {
return previous_loading_gadget_promise; return previous_loading_klass_promise;
}) })
.push(undefined, function () { .push(undefined, function () {
// Forget previous declareGadget error // Forget previous declareGadget error
...@@ -388,9 +389,25 @@ ...@@ -388,9 +389,25 @@
}) })
// Set the HTML context // Set the HTML context
.push(function (gadget_instance) { .push(function (gadget_instance) {
var i;
// 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;
return gadget_instance;
})
.push(undefined, function (e) {
// Drop the current loading klass info used by selector
// even in case of error
gadget_loading_klass = undefined;
throw e;
});
local_loading_klass_promise = loading_klass_promise;
queue = new RSVP.Queue()
.push(function () {
return local_loading_klass_promise;
})
// Set the HTML context
.push(function (gadget_instance) {
var i;
// Trigger calling of all ready callback // Trigger calling of all ready callback
function ready_wrapper() { function ready_wrapper() {
return gadget_instance; return gadget_instance;
...@@ -407,16 +424,10 @@ ...@@ -407,16 +424,10 @@
if (options.scope !== undefined) { if (options.scope !== undefined) {
parent_gadget.__sub_gadget_dict[options.scope] = gadget_instance; parent_gadget.__sub_gadget_dict[options.scope] = gadget_instance;
} }
return gadget_instance; return gadget_instance;
})
.push(undefined, function (e) {
// Drop the current loading klass info used by selector
// even in case of error
gadget_loading_klass = undefined;
throw e;
}); });
loading_gadget_promise = queue; return queue;
return loading_gadget_promise;
}) })
.declareMethod('getDeclaredGadget', function (gadget_scope) { .declareMethod('getDeclaredGadget', function (gadget_scope) {
if (!this.__sub_gadget_dict.hasOwnProperty(gadget_scope)) { if (!this.__sub_gadget_dict.hasOwnProperty(gadget_scope)) {
...@@ -715,6 +726,7 @@ ...@@ -715,6 +726,7 @@
var url = removeHash(window.location.href), var url = removeHash(window.location.href),
tmp_constructor, tmp_constructor,
root_gadget, root_gadget,
loading_gadget_promise = new RSVP.Queue(),
declare_method_count = 0, declare_method_count = 0,
embedded_channel, embedded_channel,
notifyReady, notifyReady,
...@@ -726,7 +738,7 @@ ...@@ -726,7 +738,7 @@
if (gadget_model_dict.hasOwnProperty(url)) { if (gadget_model_dict.hasOwnProperty(url)) {
throw new Error("bootstrap should not be called twice"); throw new Error("bootstrap should not be called twice");
} }
loading_gadget_promise = new RSVP.Promise(function (resolve, reject) { loading_klass_promise = new RSVP.Promise(function (resolve, reject) {
if (window.self === window.top) { if (window.self === window.top) {
last_acquisition_gadget = new RenderJSGadget(); last_acquisition_gadget = new RenderJSGadget();
...@@ -877,8 +889,7 @@ ...@@ -877,8 +889,7 @@
.then(function (all_list) { .then(function (all_list) {
var i, var i,
js_list = all_list[0], js_list = all_list[0],
css_list = all_list[1], css_list = all_list[1];
queue;
for (i = 0; i < js_list.length; i += 1) { for (i = 0; i < js_list.length; i += 1) {
javascript_registration_dict[js_list[i]] = null; javascript_registration_dict[js_list[i]] = null;
} }
...@@ -886,7 +897,24 @@ ...@@ -886,7 +897,24 @@
stylesheet_registration_dict[css_list[i]] = null; stylesheet_registration_dict[css_list[i]] = null;
} }
gadget_loading_klass = undefined; gadget_loading_klass = undefined;
queue = new RSVP.Queue(); return root_gadget;
}).then(resolve, function (e) {
reject(e);
/*global console */
console.error(e);
throw e;
});
}
document.addEventListener('DOMContentLoaded', init, false);
});
loading_gadget_promise
.push(function () {
return loading_klass_promise;
})
.push(function (root_gadget) {
var i;
function ready_wrapper() { function ready_wrapper() {
return root_gadget; return root_gadget;
} }
...@@ -903,33 +931,23 @@ ...@@ -903,33 +931,23 @@
}); });
} }
queue.push(ready_wrapper); loading_gadget_promise.push(ready_wrapper);
for (i = 0; i < tmp_constructor.__ready_list.length; i += 1) { for (i = 0; i < tmp_constructor.__ready_list.length; i += 1) {
// Put a timeout? // Put a timeout?
queue.push(tmp_constructor.__ready_list[i]) loading_gadget_promise
.push(tmp_constructor.__ready_list[i])
// Always return the gadget instance after ready function // Always return the gadget instance after ready function
.push(ready_wrapper); .push(ready_wrapper);
} }
queue.push(resolve, function (e) {
reject(e);
throw e;
});
return queue;
}).fail(function (e) {
reject(e);
/*global console */
console.error(e);
}); });
}
document.addEventListener('DOMContentLoaded', init, false);
});
if (window.self !== window.top) { if (window.self !== window.top) {
// Inform parent window that gadget is correctly loaded // Inform parent window that gadget is correctly loaded
loading_gadget_promise.then(function () { loading_gadget_promise
.then(function () {
gadget_ready = true; gadget_ready = true;
notifyReady(); notifyReady();
}).fail(function (e) { })
.fail(function (e) {
embedded_channel.notify({method: "failed", params: e.toString()}); embedded_channel.notify({method: "failed", params: e.toString()});
throw e; throw e;
}); });
......
...@@ -2110,6 +2110,40 @@ ...@@ -2110,6 +2110,40 @@
}); });
}); });
test('getDeclareGadget can be called in ready', function () {
// Subclass RenderJSGadget to not pollute its namespace
var gadget = new RenderJSGadget(),
html_url = 'https://example.org/files/qunittest/test98.html',
html_url2 = 'https://example.org/files/qunittest/test989.html';
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><body></body></html>"]);
this.server.respondWith("GET", html_url2, [200, {
"Content-Type": "text/html"
}, "<html><body></body></html>"]);
stop();
renderJS.declareGadgetKlass(html_url)
.then(function (Klass) {
// Create a ready function
Klass.ready(function (g) {
return g.declareGadget(html_url2);
});
return gadget.declareGadget(html_url);
})
.then(function () {
ok(true);
})
.fail(function (e) {
ok(false);
})
.always(function () {
start();
});
});
test('Can take a DOM element options', function () { test('Can take a DOM element options', function () {
// Subclass RenderJSGadget to not pollute its namespace // Subclass RenderJSGadget to not pollute its namespace
......
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