From 263b559b80cef3292612080716f4e0b6dc47f79d Mon Sep 17 00:00:00 2001 From: Stefano Borini Date: Tue, 20 Sep 2016 11:33:04 +0100 Subject: [PATCH 1/6] Refactoring of the javascript application table toward a MV separation --- remoteappmanager/static/js/home.js | 183 ---------------------- remoteappmanager/static/js/home/home.js | 86 ++++++++++ remoteappmanager/static/js/home/models.js | 21 +++ remoteappmanager/static/js/home/views.js | 111 +++++++++++++ remoteappmanager/templates/home.html | 2 +- 5 files changed, 219 insertions(+), 184 deletions(-) delete mode 100644 remoteappmanager/static/js/home.js create mode 100644 remoteappmanager/static/js/home/home.js create mode 100644 remoteappmanager/static/js/home/models.js create mode 100644 remoteappmanager/static/js/home/views.js diff --git a/remoteappmanager/static/js/home.js b/remoteappmanager/static/js/home.js deleted file mode 100644 index 551fcc95e..000000000 --- a/remoteappmanager/static/js/home.js +++ /dev/null @@ -1,183 +0,0 @@ -/*globals: require, console*/ -require( - ["jquery", "jhapi", "utils", "remoteappapi"], - function($, JHAPI, utils, RemoteAppAPI) { - "use strict"; - - var base_url = window.apidata.base_url; - var appapi = new RemoteAppAPI(base_url); - - // This model keeps the retrieved content from the REST query locally. - // It is only synchronized at initial load. - var application_model = []; - - var report_error = function (jqXHR, status, error) { - // Writes an error message resulting from an incorrect - // ajax operation. Parameters are from the resulting ajax. - var msg = utils.log_ajax_error(jqXHR, status, error); - $(".spawn-error-msg").text(msg).show(); - }; - - var render_applist_entry = function (index, info) { - // Returns a HTML snippet for a single application entry - // index: - // a progressive index for the entry. - // info: - // A dictionary containing the retrieved data about the application - // and (possibly) the container. - var html = '
'; - - if (info.image.icon_128 === '') { - html += ''; - } else { - html += ''; - } - - var name; - if (info.image.ui_name !== '') { - name = info.image.ui_name; - } else { - name = info.image.name; - } - html += '

'+name+'

'; - - var policy = info.image.policy; - var mount_html = ''; - - if (policy.allow_home) { - mount_html += "
  • Workspace
  • "; - } - if (policy.volume_source && policy.volume_target && policy.volume_mode) { - mount_html += "
  • "+ policy.volume_source + - " → " + - policy.volume_target + - " " + - "(" + policy.volume_mode + ")
  • "; - } - if (mount_html !== '') { - html += "
      " + mount_html + "
    "; - } - - html += '
    '; - - var cls, text, stop_style; - if (info.container !== null) { - cls = "view-button btn-success"; - text = " View"; - stop_style = ""; - } else { - cls = "start-button btn-primary"; - text = " Start"; - stop_style = 'style="visibility: hidden;"'; - } - html += '
    '; - html += ''; - html += '
    '; - html += '
    '; - html += ''; - html += '
    '; - html += '
    '; - return html; - }; - - var reset_buttons_to_start = function (index) { - // Used to revert the buttons to their "start" state when the - // User clicks on "stop". - $("#bnx_"+index) - .removeClass("view-button btn-success") - .addClass("start-button btn-primary"); - $("#bnx_"+index+" > span").text(" Start"); - $("#bny_"+index).hide(); - }; - - var render_applist = function () { - // Renders the full application list and adds it to the DOM. - var html = ""; - for (var i = 0; i < application_model.length; i++) { - var info = application_model[i]; - html += render_applist_entry(i, info); - } - $("#applist").html(html); - }; - - var x_button_clicked = function () { - // Triggered when the button X (left side) is clicked - var button = this; - var index = $(button).data("index"); - $(button).find(".fa-spinner").show(); - var app_info = application_model[index]; - - if (app_info.container !== null) { - // The container is already running, this is a View button - window.location = utils.url_path_join( - base_url, - "containers", - app_info.container.url_id); - } else { - // The container is not running. This is a start button. - var mapping_id = application_model[index].mapping_id; - appapi.start_application(mapping_id, { - error: function(jqXHR, status, error) { - report_error(jqXHR, status, error); - $(button).find(".fa-spinner").hide(); - }, - statusCode: { - 201: function (data, textStatus, request) { - var location = request.getResponseHeader('Location'); - var url = utils.parse_url(location); - var arr = url.pathname.replace(/\/$/, "").split('/'); - var url_id = arr[arr.length-1]; - $(button).find(".fa-spinner").hide(); - - window.location = utils.url_path_join( - base_url, - "containers", - url_id - ); - } - } - }); - } - }; - - var y_button_clicked = function () { - // Triggered when the button Y (right side) is clicked - var button = this; - var index = $(button).data("index"); - $(button).find(".fa-spinner").show(); - var app_info = application_model[index]; - - var url_id = app_info.container.url_id; - appapi.stop_application(url_id, { - success: function () { - $(button).find(".fa-spinner").hide(); - reset_buttons_to_start(index); - app_info.container = null; - }, - error: function (jqXHR, status, error) { - report_error(jqXHR, status, error); - $(button).find(".fa-spinner").hide(); - } - }); - }; - - var register_button_eventhandlers = function () { - // Registers the event handlers on the buttons after addition - // of the new entries to the list. - $(".bnx").click(x_button_clicked); - $(".bny").click(y_button_clicked); - }; - - var sync_local_model = function (app_data) { - application_model = app_data; - }; - - $.when(appapi.available_applications_info()) - .done(sync_local_model) - .done(render_applist) - .done(register_button_eventhandlers); - - } -); diff --git a/remoteappmanager/static/js/home/home.js b/remoteappmanager/static/js/home/home.js new file mode 100644 index 000000000..1504952aa --- /dev/null +++ b/remoteappmanager/static/js/home/home.js @@ -0,0 +1,86 @@ +/*globals: require, console*/ +require( + ["jquery", "jhapi", "utils", "remoteappapi", "home/models", "home/views"], + function($, JHAPI, utils, RemoteAppAPI, models, views) { + "use strict"; + + var base_url = window.apidata.base_url; + var appapi = new RemoteAppAPI(base_url); + + // This model keeps the retrieved content from the REST query locally. + // It is only synchronized at initial load. + var model = new models.ApplicationListModel(appapi); + var view = new views.ApplicationListView(model); + + var report_error = function (jqXHR, status, error) { + // Writes an error message resulting from an incorrect + // ajax operation. Parameters are from the resulting ajax. + var msg = utils.log_ajax_error(jqXHR, status, error); + $(".spawn-error-msg").text(msg).show(); + }; + + view.x_button_clicked = function () { + // Triggered when the button X (left side) is clicked + var button = this; + var index = $(button).data("index"); + $(button).find(".fa-spinner").show(); + var app_info = model.data[index]; + + if (app_info.container !== null) { + // The container is already running, this is a View button + window.location = utils.url_path_join( + base_url, + "containers", + app_info.container.url_id); + } else { + // The container is not running. This is a start button. + var mapping_id = model.data[index].mapping_id; + appapi.start_application(mapping_id, { + error: function(jqXHR, status, error) { + report_error(jqXHR, status, error); + $(button).find(".fa-spinner").hide(); + }, + statusCode: { + 201: function (data, textStatus, request) { + var location = request.getResponseHeader('Location'); + var url = utils.parse_url(location); + var arr = url.pathname.replace(/\/$/, "").split('/'); + var url_id = arr[arr.length-1]; + $(button).find(".fa-spinner").hide(); + + window.location = utils.url_path_join( + base_url, + "containers", + url_id + ); + } + } + }); + } + }; + + view.y_button_clicked = function () { + // Triggered when the button Y (right side) is clicked + var button = this; + var index = $(button).data("index"); + $(button).find(".fa-spinner").show(); + var app_info = model.data[index]; + + var url_id = app_info.container.url_id; + appapi.stop_application(url_id, { + success: function () { + $(button).find(".fa-spinner").hide(); + view.reset_buttons_to_start(index); + app_info.container = null; + }, + error: function (jqXHR, status, error) { + report_error(jqXHR, status, error); + $(button).find(".fa-spinner").hide(); + } + }); + }; + + $.when(model.update()).done(function () { view.render(); }); + + } +); diff --git a/remoteappmanager/static/js/home/models.js b/remoteappmanager/static/js/home/models.js new file mode 100644 index 000000000..4a95527ff --- /dev/null +++ b/remoteappmanager/static/js/home/models.js @@ -0,0 +1,21 @@ +define(['jquery'], function ($) { + "use strict"; + + var ApplicationListModel = function(remote_app_api) { + this._appapi = remote_app_api; + this.data = []; + }; + + ApplicationListModel.prototype.update = function() { + var self = this; + return $.when( + self._appapi.available_applications_info() + ).done(function (app_data) { + self.data = app_data; + }); + }; + + return { + ApplicationListModel: ApplicationListModel + }; +}); diff --git a/remoteappmanager/static/js/home/views.js b/remoteappmanager/static/js/home/views.js new file mode 100644 index 000000000..bfb75f86f --- /dev/null +++ b/remoteappmanager/static/js/home/views.js @@ -0,0 +1,111 @@ +define(["jquery", "utils"], function ($, utils) { + "use strict"; + + var base_url = window.apidata.base_url; + + var ApplicationListView = function(model) { + this.model = model; + }; + + ApplicationListView.prototype.render = function () { + // Renders the full application list and adds it to the DOM. + var html = ""; + for (var i = 0; i < this.model.data.length; i++) { + var info = this.model.data[i]; + html += this.render_applist_entry(i, info); + } + $("#applist").html(html); + this.register_button_eventhandlers() + }; + + ApplicationListView.prototype.render_applist_entry = function (index, info) { + // Returns a HTML snippet for a single application entry + // index: + // a progressive index for the entry. + // info: + // A dictionary containing the retrieved data about the application + // and (possibly) the container. + var html = '
    '; + + if (info.image.icon_128 === '') { + html += ''; + } else { + html += ''; + } + + var name; + if (info.image.ui_name !== '') { + name = info.image.ui_name; + } else { + name = info.image.name; + } + html += '

    '+name+'

    '; + + var policy = info.image.policy; + var mount_html = ''; + + if (policy.allow_home) { + mount_html += "
  • Workspace
  • "; + } + if (policy.volume_source && policy.volume_target && policy.volume_mode) { + mount_html += "
  • "+ policy.volume_source + + " → " + + policy.volume_target + + " " + + "(" + policy.volume_mode + ")
  • "; + } + if (mount_html !== '') { + html += "
      " + mount_html + "
    "; + } + + html += '
    '; + + var cls, text, stop_style; + if (info.container !== null) { + cls = "view-button btn-success"; + text = " View"; + stop_style = ""; + } else { + cls = "start-button btn-primary"; + text = " Start"; + stop_style = 'style="visibility: hidden;"'; + } + html += '
    '; + html += ''; + html += '
    '; + html += '
    '; + html += ''; + html += '
    '; + html += '
    '; + return html; + }; + + ApplicationListView.prototype.reset_buttons_to_start = function (index) { + // Used to revert the buttons to their "start" state when the + // User clicks on "stop". + $("#bnx_"+index) + .removeClass("view-button btn-success") + .addClass("start-button btn-primary"); + $("#bnx_"+index+" > span").text(" Start"); + $("#bny_"+index).hide(); + }; + + + ApplicationListView.prototype.register_button_eventhandlers = function () { + // Registers the event handlers on the buttons after addition + // of the new entries to the list. + $(".bnx").click(this.x_button_clicked); + $(".bny").click(this.y_button_clicked); + }; + + ApplicationListView.prototype.x_button_clicked = function () {}; + ApplicationListView.prototype.y_button_clicked = function () {}; + + return { + ApplicationListView : ApplicationListView + }; + + +}); diff --git a/remoteappmanager/templates/home.html b/remoteappmanager/templates/home.html index d7065422b..4caf90400 100644 --- a/remoteappmanager/templates/home.html +++ b/remoteappmanager/templates/home.html @@ -29,6 +29,6 @@

    Available Applications

    {% block script %} {% endblock %} From dc8f8f6ef9e1e0f30298ab15c7f35e437dbdfc9a Mon Sep 17 00:00:00 2001 From: Stefano Borini Date: Tue, 20 Sep 2016 14:25:02 +0100 Subject: [PATCH 2/6] Partitioning of javascript code to separate Model from View. --- remoteappmanager/static/js/home/home.js | 78 ++++++++++-------------- remoteappmanager/static/js/home/views.js | 43 ++++++++++--- 2 files changed, 68 insertions(+), 53 deletions(-) diff --git a/remoteappmanager/static/js/home/home.js b/remoteappmanager/static/js/home/home.js index 1504952aa..0ab9b8371 100644 --- a/remoteappmanager/static/js/home/home.js +++ b/remoteappmanager/static/js/home/home.js @@ -19,67 +19,53 @@ require( $(".spawn-error-msg").text(msg).show(); }; - view.x_button_clicked = function () { - // Triggered when the button X (left side) is clicked - var button = this; - var index = $(button).data("index"); - $(button).find(".fa-spinner").show(); + view.view_button_clicked = function (index) { var app_info = model.data[index]; - if (app_info.container !== null) { - // The container is already running, this is a View button - window.location = utils.url_path_join( - base_url, - "containers", - app_info.container.url_id); - } else { - // The container is not running. This is a start button. - var mapping_id = model.data[index].mapping_id; - appapi.start_application(mapping_id, { - error: function(jqXHR, status, error) { - report_error(jqXHR, status, error); - $(button).find(".fa-spinner").hide(); - }, - statusCode: { - 201: function (data, textStatus, request) { - var location = request.getResponseHeader('Location'); - var url = utils.parse_url(location); - var arr = url.pathname.replace(/\/$/, "").split('/'); - var url_id = arr[arr.length-1]; - $(button).find(".fa-spinner").hide(); - - window.location = utils.url_path_join( - base_url, - "containers", - url_id - ); - } - } - }); - } + window.location = utils.url_path_join( + base_url, + "containers", + app_info.container.url_id); }; - - view.y_button_clicked = function () { - // Triggered when the button Y (right side) is clicked - var button = this; - var index = $(button).data("index"); - $(button).find(".fa-spinner").show(); + + view.stop_button_clicked = function (index) { var app_info = model.data[index]; var url_id = app_info.container.url_id; - appapi.stop_application(url_id, { + return appapi.stop_application(url_id, { success: function () { - $(button).find(".fa-spinner").hide(); view.reset_buttons_to_start(index); app_info.container = null; }, error: function (jqXHR, status, error) { report_error(jqXHR, status, error); - $(button).find(".fa-spinner").hide(); + }}); + }; + + view.start_button_clicked = function (index) { + // The container is not running. This is a start button. + var mapping_id = model.data[index].mapping_id; + return appapi.start_application(mapping_id, { + error: function(jqXHR, status, error) { + report_error(jqXHR, status, error); + }, + statusCode: { + 201: function (data, textStatus, request) { + var location = request.getResponseHeader('Location'); + var url = utils.parse_url(location); + var arr = url.pathname.replace(/\/$/, "").split('/'); + var url_id = arr[arr.length-1]; + + window.location = utils.url_path_join( + base_url, + "containers", + url_id + ); + } } }); }; - + $.when(model.update()).done(function () { view.render(); }); } diff --git a/remoteappmanager/static/js/home/views.js b/remoteappmanager/static/js/home/views.js index bfb75f86f..4e1537cd3 100644 --- a/remoteappmanager/static/js/home/views.js +++ b/remoteappmanager/static/js/home/views.js @@ -5,6 +5,39 @@ define(["jquery", "utils"], function ($, utils) { var ApplicationListView = function(model) { this.model = model; + var self = this; + + this.stop_button_clicked = function() {}; + this.start_button_clicked = function() {}; + this.view_button_clicked = function() {}; + + this._x_button_clicked = function () { + // Triggered when the button X (left side) is clicked + var button = this; + var index = $(button).data("index"); + $(button).find(".fa-spinner").show(); + + var app_info = self.model.data[index]; + + var hide_spinner = function () { + $(button).find(".fa-spinner").hide(); + }; + + if (app_info.container !== null) { + self.view_button_clicked(index).done(hide_spinner); + } else { + self.start_button_clicked(index).done(hide_spinner); + } + }; + + this._y_button_clicked = function () { + var button = this; + var index = $(button).data("index"); + $(button).find(".fa-spinner").show(); + self.stop_button_clicked(index).done(function () { + $(button).find(".fa-spinner").hide(); + }); + }; }; ApplicationListView.prototype.render = function () { @@ -15,7 +48,7 @@ define(["jquery", "utils"], function ($, utils) { html += this.render_applist_entry(i, info); } $("#applist").html(html); - this.register_button_eventhandlers() + this.register_button_eventhandlers(); }; ApplicationListView.prototype.render_applist_entry = function (index, info) { @@ -92,17 +125,13 @@ define(["jquery", "utils"], function ($, utils) { $("#bny_"+index).hide(); }; - ApplicationListView.prototype.register_button_eventhandlers = function () { // Registers the event handlers on the buttons after addition // of the new entries to the list. - $(".bnx").click(this.x_button_clicked); - $(".bny").click(this.y_button_clicked); + $(".bnx").click(this._x_button_clicked); + $(".bny").click(this._y_button_clicked); }; - ApplicationListView.prototype.x_button_clicked = function () {}; - ApplicationListView.prototype.y_button_clicked = function () {}; - return { ApplicationListView : ApplicationListView }; From ea6bb76b984fef518e934847c096aef16279451d Mon Sep 17 00:00:00 2001 From: Stefano Borini Date: Tue, 20 Sep 2016 16:59:41 +0100 Subject: [PATCH 3/6] Attempt at better partitioning and testing --- jstests/tests.html | 3 ++ jstests/tests/home/test_models.js | 20 +++++++++++ jstests/tests/home/test_views.js | 44 ++++++++++++++++++++++++ jstests/tests/test_utils.js | 2 +- jstests/testsuite.js | 6 ++++ remoteappmanager/static/js/home/views.js | 16 ++++----- remoteappmanager/templates/page.html | 2 +- 7 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 jstests/tests/home/test_models.js create mode 100644 jstests/tests/home/test_views.js diff --git a/jstests/tests.html b/jstests/tests.html index c0476efe6..71390c226 100644 --- a/jstests/tests.html +++ b/jstests/tests.html @@ -12,5 +12,8 @@
    +
    +
    +
    diff --git a/jstests/tests/home/test_models.js b/jstests/tests/home/test_models.js new file mode 100644 index 000000000..965237e0e --- /dev/null +++ b/jstests/tests/home/test_models.js @@ -0,0 +1,20 @@ +define(function (require) { + "use strict"; + var models = require("home/models"); + + var MockApi = function () { + this.available_applications_info = function() { + return [{}, {}]; + }; + }; + + QUnit.module("home.models"); + QUnit.test("instantiation", function (assert) { + var mock_api = new MockApi(); + var model = new models.ApplicationListModel(mock_api); + assert.equal(model.data.length, 0); + model.update().done(function() { + assert.equal(model.data.length, 2); + }); + }); +}); diff --git a/jstests/tests/home/test_views.js b/jstests/tests/home/test_views.js new file mode 100644 index 000000000..61e95bff2 --- /dev/null +++ b/jstests/tests/home/test_views.js @@ -0,0 +1,44 @@ +define(function (require) { + "use strict"; + var models = require("home/models"); + var views = require("home/views"); + var $ = require("jquery"); + + var MockApi = function () { + this.available_applications_info = function() { + return [ + { + image : { + ui_name : "foo", + policy : { + allow_home: true + } + } + }, + { + image: { + ui_name : "bar", + policy : { + allow_home: true + } + } + }]; + }; + }; + + QUnit.module("home.views"); + QUnit.test("rendering", function (assert) { + var mock_api = new MockApi(); + var model = new models.ApplicationListModel(mock_api); + var view = new views.ApplicationListView(model); + model.update() + .done(function() { view.render(); } ) + .done(function() { + var applist = $("#applist"); + assert.equal(applist.children().length, 2); + assert.equal($("#applist > div:nth-child(1) > div > h4").text(), "foo"); + assert.equal($("#applist > div:nth-child(2) > div > h4").text(), "bar"); + }); + + }); +}); diff --git a/jstests/tests/test_utils.js b/jstests/tests/test_utils.js index 07171eee7..8b0da38aa 100644 --- a/jstests/tests/test_utils.js +++ b/jstests/tests/test_utils.js @@ -3,6 +3,6 @@ define(function (require) { QUnit.module("Utils"); QUnit.test("url_path_join", function (assert) { - assert.equal(utils.url_path_join("foo", "bar", "baz"), "foo/bar/baz") + assert.equal(utils.url_path_join("foo", "bar", "baz"), "foo/bar/baz"); }); }); diff --git a/jstests/testsuite.js b/jstests/testsuite.js index 3313dd95b..78d3c9444 100644 --- a/jstests/testsuite.js +++ b/jstests/testsuite.js @@ -16,9 +16,15 @@ }); require([ + "tests/home/test_models.js", + "tests/home/test_views.js", "tests/test_remoteappapi.js", "tests/test_utils.js" ], function() { + window.apidata = { + base_url: "/", + prefix: "/" + }; QUnit.load(); QUnit.start(); }); diff --git a/remoteappmanager/static/js/home/views.js b/remoteappmanager/static/js/home/views.js index 4e1537cd3..acc673f0b 100644 --- a/remoteappmanager/static/js/home/views.js +++ b/remoteappmanager/static/js/home/views.js @@ -1,11 +1,11 @@ -define(["jquery", "utils"], function ($, utils) { +define(["jquery", "utils", "initialization"], function ($, utils, initialization) { "use strict"; - var base_url = window.apidata.base_url; - var ApplicationListView = function(model) { this.model = model; var self = this; + + self.base_url = window.apidata.base_url; this.stop_button_clicked = function() {}; this.start_button_clicked = function() {}; @@ -60,16 +60,16 @@ define(["jquery", "utils"], function ($, utils) { // and (possibly) the container. var html = '
    '; - if (info.image.icon_128 === '') { + if (info.image.icon_128) { + html += ''; + } else { html += ''; - } else { - html += ''; } var name; - if (info.image.ui_name !== '') { + if (info.image.ui_name) { name = info.image.ui_name; } else { name = info.image.name; diff --git a/remoteappmanager/templates/page.html b/remoteappmanager/templates/page.html index ec4bcbc41..8ecee5c09 100644 --- a/remoteappmanager/templates/page.html +++ b/remoteappmanager/templates/page.html @@ -51,7 +51,7 @@ bootstrap: { deps: ["jquery"], exports: "bootstrap" - }, + } } }); From 67e404ca24a633068392f25496f40637c362a825 Mon Sep 17 00:00:00 2001 From: Stefano Borini Date: Tue, 20 Sep 2016 17:00:50 +0100 Subject: [PATCH 4/6] Removed initialization deps --- remoteappmanager/static/js/home/views.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/remoteappmanager/static/js/home/views.js b/remoteappmanager/static/js/home/views.js index acc673f0b..9d9eb3284 100644 --- a/remoteappmanager/static/js/home/views.js +++ b/remoteappmanager/static/js/home/views.js @@ -1,4 +1,4 @@ -define(["jquery", "utils", "initialization"], function ($, utils, initialization) { +define(["jquery", "utils"], function ($, utils) { "use strict"; var ApplicationListView = function(model) { From d9364030a371a3b1847d1a35aed91a2caa9e190e Mon Sep 17 00:00:00 2001 From: Stefano Borini Date: Tue, 20 Sep 2016 17:10:16 +0100 Subject: [PATCH 5/6] Removed JHAPI no longer needed --- remoteappmanager/static/js/home/home.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/remoteappmanager/static/js/home/home.js b/remoteappmanager/static/js/home/home.js index 0ab9b8371..1cf9d3d27 100644 --- a/remoteappmanager/static/js/home/home.js +++ b/remoteappmanager/static/js/home/home.js @@ -1,7 +1,7 @@ /*globals: require, console*/ require( - ["jquery", "jhapi", "utils", "remoteappapi", "home/models", "home/views"], - function($, JHAPI, utils, RemoteAppAPI, models, views) { + ["jquery", "utils", "remoteappapi", "home/models", "home/views"], + function($, utils, RemoteAppAPI, models, views) { "use strict"; var base_url = window.apidata.base_url; From 1fb52e7883ab7f6aa1d98f8f73efacdb292e39f0 Mon Sep 17 00:00:00 2001 From: Stefano Borini Date: Wed, 21 Sep 2016 10:01:09 +0100 Subject: [PATCH 6/6] Added comments and reformats --- jstests/testsuite.js | 4 ++-- remoteappmanager/static/js/home/models.js | 11 +++++++++++ remoteappmanager/static/js/home/views.js | 15 ++++++++++++--- remoteappmanager/static/js/remoteappapi.js | 14 +++++++------- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/jstests/testsuite.js b/jstests/testsuite.js index 78d3c9444..33d06a1a8 100644 --- a/jstests/testsuite.js +++ b/jstests/testsuite.js @@ -18,8 +18,8 @@ require([ "tests/home/test_models.js", "tests/home/test_views.js", - "tests/test_remoteappapi.js", - "tests/test_utils.js" + "tests/test_remoteappapi.js", + "tests/test_utils.js" ], function() { window.apidata = { base_url: "/", diff --git a/remoteappmanager/static/js/home/models.js b/remoteappmanager/static/js/home/models.js index 4a95527ff..3324542d5 100644 --- a/remoteappmanager/static/js/home/models.js +++ b/remoteappmanager/static/js/home/models.js @@ -2,11 +2,22 @@ define(['jquery'], function ($) { "use strict"; var ApplicationListModel = function(remote_app_api) { + // (constructor) Model for the application list. + // Parameters + // remote_app_api : RemoteAppAPI + // The remote application API stub object this._appapi = remote_app_api; + + // Contains the data retrieved from the remote API this.data = []; }; ApplicationListModel.prototype.update = function() { + // Requests an update of the object internal data. + // This method returns a jQuery Promise object. + // When resolved, this.data will contain a list of the retrieved + // data. Note that, in error conditions, this routine resolves + // successfully in any case, and the data is set to empty list var self = this; return $.when( self._appapi.available_applications_info() diff --git a/remoteappmanager/static/js/home/views.js b/remoteappmanager/static/js/home/views.js index 9d9eb3284..a7c7d9955 100644 --- a/remoteappmanager/static/js/home/views.js +++ b/remoteappmanager/static/js/home/views.js @@ -2,14 +2,23 @@ define(["jquery", "utils"], function ($, utils) { "use strict"; var ApplicationListView = function(model) { + // (Constructor) Represents the application list. In charge of + // rendering in on the div with id #applist + // + // Parameters + // model : ApplicationListModel + // The data model. this.model = model; var self = this; self.base_url = window.apidata.base_url; - this.stop_button_clicked = function() {}; - this.start_button_clicked = function() {}; - this.view_button_clicked = function() {}; + // Handlers for button clicking. + // replace them to override default behavior (doing nothing). + // Can return a value or a promise. + this.stop_button_clicked = function(index) {}; + this.start_button_clicked = function(index) {}; + this.view_button_clicked = function(index) {}; this._x_button_clicked = function () { // Triggered when the button X (left side) is clicked diff --git a/remoteappmanager/static/js/remoteappapi.js b/remoteappmanager/static/js/remoteappapi.js index 29ced8c73..e865fae2c 100644 --- a/remoteappmanager/static/js/remoteappapi.js +++ b/remoteappmanager/static/js/remoteappapi.js @@ -114,14 +114,14 @@ define(['jquery', 'utils'], function ($, utils) { .done(function (promises) { // Fills the local application model with the results of the // retrieve promises. - var data = []; - for (var i = 0; i < promises.length; i++) { - var result = promises[i]; - if (result[2].status === 200) { - data.push(result[0]); - } + var data = []; + for (var i = 0; i < promises.length; i++) { + var result = promises[i]; + if (result[2].status === 200) { + data.push(result[0]); } - promise.resolve(data); + } + promise.resolve(data); }); });