Commit 6b9e07ad authored by Ivan Tyagov's avatar Ivan Tyagov

Pass all arguments from url to handler function.

Allow getSelfGadget to be used in handler function.
Allow to define multiple levels of urls.
parent f3c2b113
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<li><a href="#/color/150/150/150/" class="list-item" style="background-color:rgb(150,150,150);">&nbsp;</a></li> <li><a href="#/color/150/150/150/" class="list-item" style="background-color:rgb(150,150,150);">&nbsp;</a></li>
<li style="text-align: center;"><a href="#/color/X/X/X" style="text-decoration: none; display: block; width: 2em;">XXX</a></li> <li style="text-align: center;"><a href="#/color/X/X/X" style="text-decoration: none; display: block; width: 2em;">XXX</a></li>
</ul> </ul>
<div class="select-color" style="display: block;">Unknown color (/color/)</div> <div class="select-color" style="display: block;"></div>
<div class="container"></div> <div class="container"></div>
...@@ -34,19 +34,18 @@ ...@@ -34,19 +34,18 @@
$.url.redirect('/color/'); $.url.redirect('/color/');
}; };
gadget.initRoutes = function () { gadget.selectColorMessage = function () {
// Create sub routed in the container $('.select-color').text("Please select a color");
// XXX: it uses low level API };
RenderJs.RouteGadget.add("/color/",
function () { gadget.updateColor = function (red, green, blue) {
$('.select-color').text("Please select a color");},
2);
RenderJs.RouteGadget.add("/color/<int:red>/<int:green>/<int:blue>/",
function (red, green, blue) {
$('.select-color').html( $('.select-color').html(
"<div style='background-color:rgb(" + red + "," + green + "," + blue + ");'>&nbsp;<\/div>" + "<div style='background-color:rgb(" + red + "," + green + "," + blue + ");'>&nbsp;<\/div>" +
"<p>Color (" + red + "," + green + "," + blue + ") selected at " + new Date() + "<\/p>");}, "<p>Color (" + red + "," + green + "," + blue + ") selected at " + new Date() + "<\/p>");
2); };
gadget.initRoutes = function () {
// XXX: improve this part so we can declare in RouteGadget
RenderJs.RouteGadget.go($.url.getPath(), RenderJs.RouteGadget.go($.url.getPath(),
function () { function () {
$('.select-color').html("Unknown color (" + $.url.getPath() + ")");}, $('.select-color').html("Unknown color (" + $.url.getPath() + ")");},
......
...@@ -7,11 +7,10 @@ ...@@ -7,11 +7,10 @@
//<![CDATA[ //<![CDATA[
$(document).ready(function() { $(document).ready(function() {
gadget = RenderJs.getSelfGadget(); gadget = RenderJs.getSelfGadget();
//console.log($(gadget.getDom()).find(".gadget-id-viewer"));
$(gadget.getDom()).find(".gadget-id-viewer").html("This is " + gadget.getId()) $(gadget.getDom()).find(".gadget-id-viewer").html("This is " + gadget.getId())
gadget.render = function (gadget_id){ gadget.render = function (){
gadget = RenderJs.GadgetIndex.getGadgetById(gadget_id); gadget = RenderJs.getSelfGadget();
$(gadget.getDom().find(".gadget-id-viewer")).toggle(); $(gadget.getDom().find(".gadget-id-viewer")).toggle();
}; };
}); });
......
...@@ -20,6 +20,12 @@ ...@@ -20,6 +20,12 @@
data-gadget-route="[ data-gadget-route="[
{&quot;source&quot;: &quot;&quot;, &quot;destination&quot;: &quot;gadget-color-picker.redirect&quot;}, {&quot;source&quot;: &quot;&quot;, &quot;destination&quot;: &quot;gadget-color-picker.redirect&quot;},
{&quot;source&quot;: &quot;/color<path:params>&quot;, &quot;destination&quot;: &quot;gadget-color-picker.initRoutes&quot;}, {&quot;source&quot;: &quot;/color<path:params>&quot;, &quot;destination&quot;: &quot;gadget-color-picker.initRoutes&quot;},
{&quot;source&quot;: &quot;/color/&quot;,
&quot;destination&quot;: &quot;gadget-color-picker.selectColorMessage&quot;,
&quot;priority&quot;:2},
{&quot;source&quot;: &quot;/color/<int:red>/<int:green>/<int:blue>/&quot;,
&quot;destination&quot;: &quot;gadget-color-picker.updateColor&quot;,
&quot;priority&quot;:2},
{&quot;source&quot;: &quot;/gadget-one/&quot;, &quot;destination&quot;: &quot;main-router.gadget_one&quot;}, {&quot;source&quot;: &quot;/gadget-one/&quot;, &quot;destination&quot;: &quot;main-router.gadget_one&quot;},
{&quot;source&quot;: &quot;/gadget-two/&quot;, &quot;destination&quot;: &quot;main-router.gadget_two&quot;}]"> {&quot;source&quot;: &quot;/gadget-two/&quot;, &quot;destination&quot;: &quot;main-router.gadget_two&quot;}]">
</div> </div>
......
...@@ -786,7 +786,7 @@ var RenderJs = (function () { ...@@ -786,7 +786,7 @@ var RenderJs = (function () {
* Create routes between gadgets. * Create routes between gadgets.
*/ */
var body = $("body"), var body = $("body"),
handler_func, handler_func, priority,
gadget_route_list = gadget_dom.attr("data-gadget-route"); gadget_route_list = gadget_dom.attr("data-gadget-route");
gadget_route_list = $.parseJSON(gadget_route_list); gadget_route_list = $.parseJSON(gadget_route_list);
$.each(gadget_route_list, function (key, gadget_route) { $.each(gadget_route_list, function (key, gadget_route) {
...@@ -794,11 +794,19 @@ var RenderJs = (function () { ...@@ -794,11 +794,19 @@ var RenderJs = (function () {
var gadget_id = gadget_route.destination.split('.')[0], var gadget_id = gadget_route.destination.split('.')[0],
method_id = gadget_route.destination.split('.')[1], method_id = gadget_route.destination.split('.')[1],
gadget = RenderJs.GadgetIndex.getGadgetById(gadget_id); gadget = RenderJs.GadgetIndex.getGadgetById(gadget_id);
// XXX: pass passed to us arguments + gadget_id // set gadget value so getSelfGadget can work
gadget[method_id](gadget_id=gadget_id); current_gadget = gadget;
gadget[method_id].apply(null, arguments);
// reset as no longer needed
current_gadget = undefined;
}; };
// add route itself // add route itself
RenderJs.RouteGadget.add(gadget_route.source, handler_func, 1); priority = gadget_route.priority;
if (priority === undefined) {
// default is 1 -i.e.first level
priority = 1;
}
RenderJs.RouteGadget.add(gadget_route.source, handler_func, priority);
}); });
}, },
......
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