Commit 3a6b374c authored by Ivan Tyagov's avatar Ivan Tyagov

Simlify and remove old unused functions. Set more meaningful function

names.
Extend docstrings and remove unused code in tests.
parent 983b271c
...@@ -25,6 +25,7 @@ var RenderJs = (function () { ...@@ -25,6 +25,7 @@ var RenderJs = (function () {
var is_ready = false; var is_ready = false;
return { return {
init: function () { init: function () {
/* /*
* Do all initialization * Do all initialization
...@@ -50,19 +51,24 @@ var RenderJs = (function () { ...@@ -50,19 +51,24 @@ var RenderJs = (function () {
}, },
bootstrap: function (root) { bootstrap: function (root) {
/* initial load application gadget */ /*
* Load all gadgets for this DOM element
* (including recursively contained ones)
*/
var gadget_id, is_gadget; var gadget_id, is_gadget;
gadget_id = root.attr("id"); gadget_id = root.attr("id");
is_gadget = root.attr("data-gadget")!==undefined; is_gadget = root.attr("data-gadget")!==undefined;
if (is_gadget && gadget_id!==undefined ) { if (is_gadget && gadget_id!==undefined ) {
// bootstart root gadget only if it is indeed a gadget // bootstart root gadget only if it is indeed a gadget
RenderJs.loadGadgetFromUrl(root); RenderJs.loadGadget(root);
} }
RenderJs.load(root); RenderJs.loadRecursiveGadget(root);
}, },
load: function (root) { loadRecursiveGadget: function (root) {
/* Load gadget layout by traversing DOM */ /*
* Load all contained gadgets inside passed DOM element.
*/
var gadget_list, gadget, gadget_id, gadget_js; var gadget_list, gadget, gadget_id, gadget_js;
gadget_list = root.find("[data-gadget]"); gadget_list = root.find("[data-gadget]");
...@@ -77,19 +83,24 @@ var RenderJs = (function () { ...@@ -77,19 +83,24 @@ var RenderJs = (function () {
// Load chilren // Load chilren
gadget_list.each(function () { gadget_list.each(function () {
RenderJs.loadGadgetFromUrl($(this)); RenderJs.loadGadget($(this));
}); });
}, },
updateAndRecurse: function (gadget, data) { setGadgetAndRecurse: function (gadget, data) {
/* Update current gadget and recurse down */ /*
* Set gadget data and recursively load it in case it holds another
* gadgets.
*/
gadget.append(data); gadget.append(data);
// a gadget may contain sub gadgets // a gadget may contain sub gadgets
this.load(gadget); RenderJs.loadRecursiveGadget(gadget);
}, },
loadGadgetFromUrl: function (gadget) { loadGadget: function (gadget) {
/* 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;
url = gadget.attr("data-gadget"); url = gadget.attr("data-gadget");
...@@ -135,7 +146,7 @@ var RenderJs = (function () { ...@@ -135,7 +146,7 @@ var RenderJs = (function () {
RenderJs.Cache.set(cache_id, data); RenderJs.Cache.set(cache_id, data);
RenderJs.GadgetIndex.getGadgetById(gadget_id). RenderJs.GadgetIndex.getGadgetById(gadget_id).
setReady(); setReady();
RenderJs.updateAndRecurse(gadget, data); RenderJs.setGadgetAndRecurse(gadget, data);
RenderJs.checkAndTriggerReady(); RenderJs.checkAndTriggerReady();
} }
}); });
...@@ -143,7 +154,7 @@ var RenderJs = (function () { ...@@ -143,7 +154,7 @@ var RenderJs = (function () {
// get from cache // get from cache
data = app_cache; data = app_cache;
gadget_js.setReady(); gadget_js.setReady();
this.updateAndRecurse(gadget, data); this.setGadgetAndRecurse(gadget, data);
this.checkAndTriggerReady(); this.checkAndTriggerReady();
} }
} else { } else {
...@@ -155,7 +166,7 @@ var RenderJs = (function () { ...@@ -155,7 +166,7 @@ var RenderJs = (function () {
gadget_id = this.yourCustomData.gadget_id; gadget_id = this.yourCustomData.gadget_id;
RenderJs.GadgetIndex.getGadgetById(gadget_id). RenderJs.GadgetIndex.getGadgetById(gadget_id).
setReady(); setReady();
RenderJs.updateAndRecurse(gadget, data); RenderJs.setGadgetAndRecurse(gadget, data);
RenderJs.checkAndTriggerReady(); RenderJs.checkAndTriggerReady();
} }
}); });
...@@ -207,23 +218,11 @@ var RenderJs = (function () { ...@@ -207,23 +218,11 @@ var RenderJs = (function () {
return is_gadget_list_loaded; return is_gadget_list_loaded;
}, },
update: function (root) {
/* update gadget with data from remote source */
root.find("[gadget]").each(function (i, v) {
RenderJs.updateGadgetData($(this));
});
},
updateGadgetWithDataHandler: function (result) {
var data_handler;
data_handler = this.yourCustomData.data_handler;
if (data_handler !== undefined) {
eval(data_handler + "(result)");
}
},
updateGadgetData: function (gadget) { updateGadgetData: function (gadget) {
/* Do real gagdet update here */ /*
* Gadget can be updated from "data-gadget-source" (i.e. a json)
* and "data-gadget-handler" attributes (i.e. a namespace Javascript)
*/
var data_source, data_handler; var data_source, data_handler;
data_source = gadget.attr("data-gadget-source"); data_source = gadget.attr("data-gadget-source");
data_handler = gadget.attr("data-gadget-handler"); data_handler = gadget.attr("data-gadget-handler");
...@@ -233,7 +232,12 @@ var RenderJs = (function () { ...@@ -233,7 +232,12 @@ var RenderJs = (function () {
url: data_source, url: data_source,
dataType: "json", dataType: "json",
yourCustomData: {"data_handler": data_handler}, yourCustomData: {"data_handler": data_handler},
success: RenderJs.updateGadgetWithDataHandler success: function (result) {
var data_handler;
data_handler = this.yourCustomData.data_handler;
if (data_handler !== undefined) {
eval(data_handler + "(result)");}
}
}); });
} }
}, },
......
...@@ -38,7 +38,6 @@ function setupRenderJSTest(){ ...@@ -38,7 +38,6 @@ function setupRenderJSTest(){
stop(); stop();
RenderJs.bindReady(function (){ RenderJs.bindReady(function (){
RenderJs.update($("#qunit-fixture"));
start(); start();
equal(RenderJs.GadgetIndex.getGadgetList().length, 1); equal(RenderJs.GadgetIndex.getGadgetList().length, 1);
equal(true, RenderJs.GadgetIndex.isGadgetListLoaded()); equal(true, RenderJs.GadgetIndex.isGadgetListLoaded());
...@@ -72,9 +71,6 @@ function setupRenderJSTest(){ ...@@ -72,9 +71,6 @@ function setupRenderJSTest(){
cleanUp(); cleanUp();
$("#qunit-fixture").append('<div data-gadget="" id="new-init" data-gadget-property="{&quot;name&quot;: &quot;Ivan&quot;, &quot;age&quot;: 33}">X</div>'); $("#qunit-fixture").append('<div data-gadget="" id="new-init" data-gadget-property="{&quot;name&quot;: &quot;Ivan&quot;, &quot;age&quot;: 33}">X</div>');
RenderJs.bootstrap($("#qunit-fixture")); RenderJs.bootstrap($("#qunit-fixture"));
RenderJs.bindReady(function (){
RenderJs.update($("#qunit-fixture"));
});
// test that gadget get a proper initialization from data-gadget-property // test that gadget get a proper initialization from data-gadget-property
equal('Ivan', RenderJs.GadgetIndex.getGadgetById("new-init").name); equal('Ivan', RenderJs.GadgetIndex.getGadgetById("new-init").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