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