Skip to content

Commit

Permalink
convert menu:query to request and have it reply with a promise
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1g5 committed May 23, 2024
1 parent bc6e2cf commit 7e8f6c6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -746,20 +746,27 @@ hqDefine("cloudcare/js/formplayer/menus/views/query", [

performSubmit: function (initiatedBy) {
var self = this;
self.validateAllFields().done(function () {
FormplayerFrontend.trigger(
"menu:query",
self.getAnswers(),
self.options.sidebarEnabled,
initiatedBy
);
if (self.smallScreenEnabled && self.options.sidebarEnabled) {
$('#sidebar-region').collapse('hide');
}
sessionStorage.submitPerformed = true;
self.executeSearch(initiatedBy).done(function (response) {
self.updateModels(response);
self.displayErrors();
});
},

executeSearch: function (initiatedBy) {
var request = FormplayerFrontend.getChannel().request(
"menu:query",
self.getAnswers(),
self.options.sidebarEnabled,
initiatedBy
);

if (self.smallScreenEnabled && self.options.sidebarEnabled) {
$('#sidebar-region').collapse('hide');
}
sessionStorage.submitPerformed = true;
return request;
},

updateSearchResults: function () {
var self = this;
var invalidRequiredFields = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ hqDefine("cloudcare/js/formplayer/router", [
// We can't do any menu navigation without an appId
FormplayerFrontend.trigger("apps:list");
} else {
menusController.selectMenu(urlObject);
return menusController.selectMenu(urlObject);
}
},
listUsers: function (page, query) {
Expand Down Expand Up @@ -226,7 +226,7 @@ hqDefine("cloudcare/js/formplayer/router", [
API.listMenus();
});

FormplayerFrontend.on("menu:query", function (queryDict, sidebarEnabled, initiatedByTag) {
FormplayerFrontend.getChannel().reply("menu:query", function (queryDict, sidebarEnabled, initiatedByTag) {
var urlObject = utils.currentUrlToObject();
var queryObject = _.extend(
{
Expand All @@ -243,7 +243,7 @@ hqDefine("cloudcare/js/formplayer/router", [
urlObject.setRequestInitiatedByTag(initiatedByTag);
let encodedUrl = utils.objectToEncodedUrl(urlObject.toJson());
sessionStorage.removeItem('selectedValues');
API.listMenus(encodedUrl);
return API.listMenus(encodedUrl);
});

FormplayerFrontend.on('restore_as:list', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ hqDefine("cloudcare/js/formplayer/spec/utils_spec", [
assert.equal(response.type, "query");
assert.deepEqual(_.pluck(response.displays, 'id'), ['dob']);

FormplayerFrontend.trigger("menu:query", {dob: "2010-01-19"});
FormplayerFrontend.getChannel().request("menu:query", {dob: "2010-01-19"});
let url = Utils.currentUrlToObject();
assert.deepEqual(url.selections, ['1', 'action 0']);
assert.deepEqual(_.keys(url.queryData), ["search_command.m1"]);
Expand Down
2 changes: 1 addition & 1 deletion docs/web_apps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ Marionette `integrates with Backbone.Radio <https://marionettejs.com/docs/master

Although you can namespace channels, web apps uses a single ``formplayer`` channel for all messages, which is accessed using ``FormplayerFrontend.getChannel()``. You'll see calls to get the channel and then call ``request`` to get at a variety of global-esque data, especially the current user. All of these requests are handled by ``reply`` callbacks defined in ``FormplayerFrontend``.

``FormplayerFrontend`` also supports events, which behave similarly. Events are triggered directly on the ``FormplayerFrontend`` object, which defines ``on`` handlers. We tend to use events for navigation and do namespace some of them with ``:``, leading to events like ``menu:select``, ``menu:query``, and ``menu:show:detail``. Some helper events are not namespaced, such as ``showError`` and ``showSuccess``.
``FormplayerFrontend`` also supports events, which behave similarly. Events are triggered directly on the ``FormplayerFrontend`` object, which defines ``on`` handlers. We tend to use events for navigation and do namespace some of them with ``:``, leading to events like ``menu:select`` and ``menu:show:detail``. Some helper events are not namespaced, such as ``showError`` and ``showSuccess``.

Routing, URLs, and Middleware
=============================
Expand Down

0 comments on commit 7e8f6c6

Please sign in to comment.