Commit 384df48f authored by Ivan Tyagov's avatar Ivan Tyagov

Extend RouteGadget. Make use of new API in examples.

parent 09817dc3
...@@ -10,20 +10,16 @@ ...@@ -10,20 +10,16 @@
// dirty way to get which is actual gadget we are used for // dirty way to get which is actual gadget we are used for
gadget_id = $(".body").parent().attr("id") gadget_id = $(".body").parent().attr("id")
gadget = RenderJs.GadgetIndex.getGadgetById(gadget_id); gadget = RenderJs.GadgetIndex.getGadgetById(gadget_id);
// define entry point which handles an url
gadget.render = function (){ gadget.render = function (){
var body = $(".body"); var body = $(".body"), handler_func;
// Home route. Redirect to sub app /color/
body
.route("add", "", 1)
.done(function () {
// Default route. Redirect to color subapp // Default route. Redirect to color subapp
$.url.redirect('/color/'); RenderJs.RouteGadget.add("",
}); function () {$.url.redirect('/color/');}, 1);
// /color app. Create subroutes and initialize DOM handler_func = function () {
body
.route("add", "/color<path:params>", 1)
.done(function () {
var i, j, k, increment = 150, page, container; var i, j, k, increment = 150, page, container;
// Container for the selected color // Container for the selected color
page = "<a href='" + $.url.generateUrl("unknown") + "'>Wrong global route<a>"; page = "<a href='" + $.url.generateUrl("unknown") + "'>Wrong global route<a>";
...@@ -45,29 +41,27 @@ ...@@ -45,29 +41,27 @@
page += "<a href='" + $.url.generateUrl("/gadget-one/") + "'>Gadget 1</a>"; page += "<a href='" + $.url.generateUrl("/gadget-one/") + "'>Gadget 1</a>";
page += "&nbsp;<a href='" + $.url.generateUrl("/gadget-two/") + "'>Gadget 2</a>"; page += "&nbsp;<a href='" + $.url.generateUrl("/gadget-two/") + "'>Gadget 2</a>";
page += "<div class='container'></div>"; page += "<div class='container'></div>";
$('.body').html(page); body.html(page);
// Create sub routed in the container // Create sub routed in the container
container = $(this).find("div"); RenderJs.RouteGadget.add("/color/",
container function () {
.route("add", "/color/", 2) $('.select-color').text("Please select a color");}, 2);
.done(function () { RenderJs.RouteGadget.add("/color/<int:red>/<int:green>/<int:blue>/",
$('.select-color').text("Please select a color"); function (red, green, blue) {
});
container
.route("add", "/color/<int:red>/<int:green>/<int:blue>/", 2)
.done(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);
}); RenderJs.RouteGadget.go($.url.getPath(),
container function () {
.route("go", $.url.getPath(), 2) $('.select-color').html("Unknown color (" + $.url.getPath() + ")");},
.fail(function () { 2);
$('.select-color').html("Unknown color (" + $.url.getPath() + ")"); };
});
}); // /color app. Create subroutes and initialize DOM
RenderJs.RouteGadget.add("/color<path:params>", handler_func, 1);
}; };
}); });
//]]> //]]>
......
...@@ -18,29 +18,27 @@ require([ "renderjs", "require-renderjs", "jquery", "route", "url" ], function(d ...@@ -18,29 +18,27 @@ require([ "renderjs", "require-renderjs", "jquery", "route", "url" ], function(d
// XXX: try to encapsulate this in router gadget // XXX: try to encapsulate this in router gadget
gadget = RenderJs.GadgetIndex.getGadgetById("main-router"); gadget = RenderJs.GadgetIndex.getGadgetById("main-router");
gadget.gadget_one = function (){ gadget.gadget_one = function (){
console.log("gadget-one");
// we use interactionGadget which will call proper gadgets' function // we use interactionGadget which will call proper gadgets' function
}; };
gadget = RenderJs.GadgetIndex.getGadgetById("main-router"); gadget = RenderJs.GadgetIndex.getGadgetById("main-router");
gadget.gadget_two = function (){ gadget.gadget_two = function (){
console.log("gadget-two");
// we use interactionGadget which will call proper gadgets' function // we use interactionGadget which will call proper gadgets' function
}; };
// XXX: fid why interaction gadget is not initialized proeprly yet // XXX: fid why interaction gadget is not initialized properly yet
RenderJs.InteractionGadget.bind($("#main-interactor")) $("div[data-gadget-connection]").each(function (index, element) {
RenderJs.InteractionGadget.bind($(element));
});
var body = $("body");
RenderJs.GadgetIndex.getGadgetById("gadget-color-picker").render(); RenderJs.GadgetIndex.getGadgetById("gadget-color-picker").render();
$.url.onhashchange(function () { $.url.onhashchange(function () {
body RenderJs.RouteGadget.go($.url.getPath(),
.route("go", $.url.getPath()) function () {
.fail(function () {
// Method to display error to the user // Method to display error to the user
$(this).html( $(this).html(
"<p>Oups, seems the route '<b>" + $.url.getPath() + "<\/b>' doesn't exist!<\/p>" + "<p>Oups, seems the route '<b>" + $.url.getPath() + "<\/b>' doesn't exist!<\/p>" +
"<a href='" + $.url.generateUrl("") + "'>Go back to home<\/a>" "<a href='" + $.url.generateUrl("") + "'>Go back to home<\/a>");
);
// All routes have been deleted by fail. // All routes have been deleted by fail.
// Recreate the default one. // Recreate the default one.
//initialize_route.apply(this, []); //initialize_route.apply(this, []);
......
...@@ -768,19 +768,39 @@ var RenderJs = (function () { ...@@ -768,19 +768,39 @@ var RenderJs = (function () {
* Create routes between gadgets. * Create routes between gadgets.
*/ */
var body = $("body"), var body = $("body"),
handler_func,
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) {
// add route itself handler_func = function () {
body
.route("add", gadget_route.source, 1)
.done(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);
gadget[method_id](); gadget[method_id]();
};
// add route itself
RenderJs.RouteGadget.add(gadget_route.source, handler_func, 1);
}); });
}); },
add: function (path, handler_func, priority) {
/*
* Add a route between path (hashable) and a handler function (part of Gadget's API).
*/
var body = $("body");
body
.route("add", path, 1)
.done(handler_func);
},
go: function (path, handler_func, priority) {
/*
* Go a route.
*/
var body = $("body");
body
.route("go", path, priority)
.fail(handler_func);
} }
}; };
}()) }())
......
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