Commit 09817dc3 authored by Ivan Tyagov's avatar Ivan Tyagov

Do not use hard coded ids to get gadget instance, instead use dom tree

and traverse to parent gadget. This allos multiple gadget instances in
one page.
parent 141f1e6e
...@@ -2,14 +2,16 @@ ...@@ -2,14 +2,16 @@
<head></head> <head></head>
<body> <body>
<h2> Color picker</h2> <h2> Color picker</h2>
<div id="body"></div> <div class="body"></div>
<script type="text/javascript" language="javascript"> <script type="text/javascript" language="javascript">
//<![CDATA[ //<![CDATA[
$(document).ready(function() { $(document).ready(function() {
gadget = RenderJs.GadgetIndex.getGadgetById("gadget-color-picker"); // dirty way to get which is actual gadget we are used for
gadget_id = $(".body").parent().attr("id")
gadget = RenderJs.GadgetIndex.getGadgetById(gadget_id);
gadget.render = function (){ gadget.render = function (){
var body = $("body"); var body = $(".body");
// Home route. Redirect to sub app /color/ // Home route. Redirect to sub app /color/
body body
.route("add", "", 1) .route("add", "", 1)
...@@ -39,23 +41,23 @@ ...@@ -39,23 +41,23 @@
page += "<li style='text-align: center;'><a style='text-decoration: none; display: block; width: 2em;' href='" + page += "<li style='text-align: center;'><a style='text-decoration: none; display: block; width: 2em;' href='" +
$.url.generateUrl("/color/X/X/X") + "'>XXX<a><\/li>"; $.url.generateUrl("/color/X/X/X") + "'>XXX<a><\/li>";
page += "<\/ul>"; page += "<\/ul>";
page += "<div style='display: block;' id='select-color'><\/div>" page += "<div style='display: block;' class='select-color'><\/div>"
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 id='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"); container = $(this).find("div");
container container
.route("add", "/color/", 2) .route("add", "/color/", 2)
.done(function () { .done(function () {
$('#select-color').text("Please select a color"); $('.select-color').text("Please select a color");
}); });
container container
.route("add", "/color/<int:red>/<int:green>/<int:blue>/", 2) .route("add", "/color/<int:red>/<int:green>/<int:blue>/", 2)
.done(function (red, green, blue) { .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>"
); );
...@@ -63,7 +65,7 @@ ...@@ -63,7 +65,7 @@
container container
.route("go", $.url.getPath(), 2) .route("go", $.url.getPath(), 2)
.fail(function () { .fail(function () {
$('#select-color').html("Unknown color (" + $.url.getPath() + ")"); $('.select-color').html("Unknown color (" + $.url.getPath() + ")");
}); });
}); });
}; };
......
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