Commit 485ee200 authored by Ivan Tyagov's avatar Ivan Tyagov

Create .init methods for RouteGadget and InteractionGadget so we can

call them from gadgets without taking care for real implementation
inside. Allow force rebind for InteractionGadget.
Extend test for mentioned changes.
parent abf5797e
......@@ -24,10 +24,8 @@ require([ "renderjs", "require-renderjs", "jquery", "route", "url" ], function(d
// we use interactionGadget which will call proper gadgets' function
};
// we have to re-bind interaction gadget as main-route API implemantation changed
$("div[data-gadget-connection]").each(function (index, element) {
RenderJs.InteractionGadget.bind($(element));
});
// we have to re-bind (force) interaction gadget as main-route API implemantation changed
RenderJs.InteractionGadget.init(force=1);
$.url.onhashchange(function () {
RenderJs.RouteGadget.go($.url.getPath(),
......
......@@ -47,18 +47,11 @@ var RenderJs = (function () {
function () {
if (RENDERJS_ENABLE_IMPLICIT_INTERACTION_BIND) {
// examine all Intaction Gadgets and bind accordingly
$("div[data-gadget-connection]")
.filter(function() { return $(this).data("bound") !== true; })
.data('bound', true )
.each(function (index, element) {
RenderJs.InteractionGadget.bind($(element));
});
RenderJs.InteractionGadget.init();
}
if (RENDERJS_ENABLE_IMPLICIT_ROUTE_CREATE) {
// create all routes between gadgets
$("div[data-gadget-route]").each(function (index, element) {
RenderJs.RouteGadget.route($(element));
});
RenderJs.RouteGadget.init();
}
});
}
......@@ -687,6 +680,25 @@ var RenderJs = (function () {
* Basic gadget interaction gadget implementation.
*/
return {
init: function (force) {
/*
* Inspect DOM and initialize this gadget
*/
var dom_list;
if (force===1) {
// we explicitly want to re-init elements even if already this is done before
dom_list = $("div[data-gadget-connection]");
}
else {
dom_list = $("div[data-gadget-connection]")
.filter(function() { return $(this).data("bound") !== true; })
.data('bound', true )
}
dom_list.each(function (index, element) {
RenderJs.InteractionGadget.bind($(element));});
},
bind: function (gadget_dom) {
/*
* Bind event between gadgets.
......@@ -781,6 +793,15 @@ var RenderJs = (function () {
var route_list = [];
return {
init: function () {
/*
* Inspect DOM and initialize this gadget
*/
$("div[data-gadget-route]").each(function (index, element) {
RenderJs.RouteGadget.route($(element));
});
},
route: function (gadget_dom) {
/*
* Create routes between gadgets.
......
......@@ -159,10 +159,7 @@ function setupRenderJSTest(){
// we need to wait for all gadgets loading ...
RenderJs.bindReady(function () {
//RenderJs.InteractionGadget.bind($("#main-interactor"));
$("div[data-gadget-connection]").each(function (index, element) {
RenderJs.InteractionGadget.bind($(element));
});
RenderJs.InteractionGadget.init();
start();
equal(0, counter);
......@@ -180,6 +177,21 @@ function setupRenderJSTest(){
// check multiple interactors can coexist (a.inc2 +2 -> B.inc2 +2)
RenderJs.GadgetIndex.getGadgetById("A").inc2();
equal(10, counter);
// test force rebind
RenderJs.GadgetIndex.getGadgetById("A").inc2 = function () {return 0;};
equal(0, RenderJs.GadgetIndex.getGadgetById("A").inc2());
// rebind should not override inc2 as already changed
RenderJs.InteractionGadget.init();
equal(0, RenderJs.GadgetIndex.getGadgetById("A").inc2());
// if we force rebind it should be back to previous state
RenderJs.GadgetIndex.getGadgetById("A").inc2 = function (){counter = counter +2;};
RenderJs.InteractionGadget.init(force=1);
RenderJs.GadgetIndex.getGadgetById("A").inc2()
equal(16, counter);
// XXX: test dynamically adding an InteractionGadget
});
......@@ -254,9 +266,7 @@ function setupRenderJSTest(){
RenderJs.bindReady(function () {
start();
// initialize route gadget as it's loaded asynchronously
$("div[data-gadget-route]").each(function (index, element) {
RenderJs.RouteGadget.route($(element));
});
RenderJs.RouteGadget.init();
var path_list = [];
$.each(RenderJs.RouteGadget.getRouteList(),
function (index, value) {
......
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