Commit 457e8e95 authored by Ivan Tyagov's avatar Ivan Tyagov

Extend GadgetIndex API.

Extend addGadget API to include a mandatory ID.
Improve handling of data-gadget-source and data-gadget-handler and add a
test for this.
parent d0010f8f
...@@ -102,7 +102,7 @@ var RenderJs = (function () { ...@@ -102,7 +102,7 @@ var RenderJs = (function () {
* Load gadget's SPECs from URL * Load gadget's SPECs from URL
*/ */
var url, gadget_id, gadget_property, cacheable, cache_id, var url, gadget_id, gadget_property, cacheable, cache_id,
app_cache, data, gadget_js; app_cache, data, gadget_js, is_update_gadget_data_running;
url = gadget.attr("data-gadget"); url = gadget.attr("data-gadget");
gadget_id = gadget.attr("id"); gadget_id = gadget.attr("id");
gadget_js = RenderJs.GadgetIndex.getGadgetById(gadget_id); gadget_js = RenderJs.GadgetIndex.getGadgetById(gadget_id);
...@@ -176,8 +176,12 @@ var RenderJs = (function () { ...@@ -176,8 +176,12 @@ var RenderJs = (function () {
// gadget is an inline (InteractorGadget or one using // gadget is an inline (InteractorGadget or one using
// data-gadget-source / data-gadget-handler) so no need // data-gadget-source / data-gadget-handler) so no need
// to load it from network // to load it from network
RenderJs.updateGadgetData(gadget); is_update_gadget_data_running = RenderJs.updateGadgetData(gadget);
gadget_js.setReady(); if (!is_update_gadget_data_running){
// no update is running so gadget is basically ready
// if update is running then it should take care and set status
gadget_js.setReady();
}
RenderJs.checkAndTriggerReady(); RenderJs.checkAndTriggerReady();
} }
}, },
...@@ -216,6 +220,7 @@ var RenderJs = (function () { ...@@ -216,6 +220,7 @@ var RenderJs = (function () {
trigger("ready"); trigger("ready");
// trigger ready on root body element // trigger ready on root body element
$("body").trigger("ready"); $("body").trigger("ready");
// this set will make sure we fire this event only once
RenderJs.setReady(true); RenderJs.setReady(true);
} }
} }
...@@ -235,18 +240,30 @@ var RenderJs = (function () { ...@@ -235,18 +240,30 @@ var RenderJs = (function () {
$.ajax({ $.ajax({
url: data_source, url: data_source,
dataType: "json", dataType: "json",
yourCustomData: {"data_handler": data_handler}, yourCustomData: {"data_handler": data_handler,
"gadget_id": gadget.attr("id")},
success: function (result) { success: function (result) {
var data_handler; var data_handler, gadget_id;
data_handler = this.yourCustomData.data_handler; data_handler = this.yourCustomData.data_handler;
gadget_id = this.yourCustomData.gadget_id;
if (data_handler !== undefined) { if (data_handler !== undefined) {
eval(data_handler + "(result)");} // eval is not nice to use
eval(data_handler + "(result)");
gadget = RenderJs.GadgetIndex.getGadgetById(gadget_id);
// mark gadget as loaded and fire a check
// to see if all gadgets are loaded
gadget.setReady();
RenderJs.checkAndTriggerReady();
}
} }
}); });
// asynchronous update happens and respective thread will update status
return true;
} }
return false;
}, },
addGadget: function (dom_id, gadget, gadget_data_handler, addGadget: function (dom_id, gadget_id, gadget, gadget_data_handler,
gadget_data_source) { gadget_data_source) {
/* /*
* add new gadget and render it * add new gadget and render it
...@@ -256,6 +273,7 @@ var RenderJs = (function () { ...@@ -256,6 +273,7 @@ var RenderJs = (function () {
tab_container.empty(); tab_container.empty();
html_string = [ html_string = [
'<div class="gadget" ', '<div class="gadget" ',
'id="' + gadget_id + '"',
'data-gadget="' + gadget + '"', 'data-gadget="' + gadget + '"',
'data-gadget-handler="' + gadget_data_handler + '" ', 'data-gadget-handler="' + gadget_data_handler + '" ',
'data-gadget-source="' + gadget_data_source + '"></div>' 'data-gadget-source="' + gadget_data_source + '"></div>'
...@@ -436,6 +454,13 @@ var RenderJs = (function () { ...@@ -436,6 +454,13 @@ var RenderJs = (function () {
var gadget_list = []; var gadget_list = [];
return { return {
setGadgetList: function (gadget_list_value) {
/*
* Set list of registered gadgets
*/
gadget_list = gadget_list_value;
},
getGadgetList: function () { getGadgetList: function () {
/* /*
* Return list of registered gadgets * Return list of registered gadgets
......
{"first_name": "John",
"last_name": "Doh"}
\ No newline at end of file
/* /*
* RenderJs tests * RenderJs tests
*/ */
counter = 0;
// in tests we need to call function manually rather than rely // in tests we need to call function manually rather than rely
// on implicit calling // on implicit calling
RENDERJS_ENABLE_IMPLICIT_INTERACTION_BIND=false; RENDERJS_ENABLE_IMPLICIT_INTERACTION_BIND=false;
...@@ -11,9 +11,17 @@ function cleanUp () { ...@@ -11,9 +11,17 @@ function cleanUp () {
* Clean up namespace between tests * Clean up namespace between tests
*/ */
// re-init GadgetIndex // re-init GadgetIndex
$.each(RenderJs.GadgetIndex.getGadgetList(), function () { RenderJs.GadgetIndex.setGadgetList([]);
RenderJs.GadgetIndex.unregisterGadget(this); equal(0, RenderJs.GadgetIndex.getGadgetList().length);
}); }
// used by tests namespace variables
counter = 0;
first_name=''
last_name=''
function parseJSONAndUpdateNameSpace(result) {
first_name=result['first_name'];
last_name=result['last_name'];
} }
function setupRenderJSTest(){ function setupRenderJSTest(){
...@@ -56,7 +64,7 @@ function setupRenderJSTest(){ ...@@ -56,7 +64,7 @@ function setupRenderJSTest(){
test('addGadget', function () { test('addGadget', function () {
cleanUp(); cleanUp();
equal(RenderJs.GadgetIndex.getGadgetList().length, 0); equal(RenderJs.GadgetIndex.getGadgetList().length, 0);
RenderJs.addGadget("qunit-fixture", "loading/test-gadget.html", "", ""); RenderJs.addGadget("qunit-fixture", "new_added", "loading/test-gadget.html", "", "");
stop(); stop();
RenderJs.bindReady(function (){ RenderJs.bindReady(function (){
...@@ -81,7 +89,7 @@ function setupRenderJSTest(){ ...@@ -81,7 +89,7 @@ function setupRenderJSTest(){
module("GadgetReadyEvent"); module("GadgetReadyEvent");
test('GadgetReadyEvent', function () { test('GadgetReadyEvent', function () {
cleanUp(); cleanUp();
RenderJs.addGadget("qunit-fixture", "interactions/index.html", "", ""); RenderJs.addGadget("qunit-fixture", "new_added", "interactions/index.html", "", "");
stop(); stop();
// we need to wait for all gadgets loading ... // we need to wait for all gadgets loading ...
...@@ -94,7 +102,7 @@ function setupRenderJSTest(){ ...@@ -94,7 +102,7 @@ function setupRenderJSTest(){
module("InteractionGadget"); module("InteractionGadget");
test('InteractionGadget', function () { test('InteractionGadget', function () {
cleanUp(); cleanUp();
RenderJs.addGadget("qunit-fixture", "interactions/index.html", "", ""); RenderJs.addGadget("qunit-fixture", "new_add", "interactions/index.html", "", "");
stop(); stop();
// we need to wait for all gadgets loading ... // we need to wait for all gadgets loading ...
...@@ -110,5 +118,24 @@ function setupRenderJSTest(){ ...@@ -110,5 +118,24 @@ function setupRenderJSTest(){
equal(3, counter); equal(3, counter);
}); });
}); });
module("GadgetDataHandler");
test('GadgetDataHandler', function () {
cleanUp();
$("#qunit-fixture").append('<div data-gadget="" id="json-gadget" data-gadget-source = "json/json_file.json" data-gadget-handler="parseJSONAndUpdateNameSpace"></div>');
RenderJs.setReady(false);
RenderJs.bootstrap($("#qunit-fixture"));
equal('', first_name);
equal('', last_name);
stop();
// we need to wait for all gadgets loading ...
RenderJs.bindReady(function () {
start();
equal('John', first_name);
equal('Doh', last_name);
});
});
}; };
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