Run 2 functions in one gadget

Version 1
    This document was generated from CDN thread

    Created by: Jonathan Mendez on 23-12-2013 10:11:11 AM
    I want to run 2 functions in one gadget.

    I have 2 js files, "file1.js" and "file2.js" I include both in the xml.
    the classes in both files have different name.

    this is the code that I have to

    initializegadgets.HubSettings.onConnect = function () {                 
       finesse.modules.file1.init ();                 
       finesse.modules.file2.init ();             
    };

    but when I initialize only 1 of them starts.

    anyone know how to initialize both?


    Subject: RE: Run 2 functions in one gadget
    Replied by: Joseph Horvath on 23-12-2013 10:20:56 AM
    Place a "try - catch" around your init() calls and see if the first one throws an exception, causing the second one to not even try to run.

    1initializegadgets.HubSettings.onConnect = function () {    
    2   try {          
    3   finesse.modules.file1.init ();                
    4   finesse.modules.file2.init ();
    5   } catch (ex) {
    6       alert ("Exception: " + ex.message + " " + ex.stack);
    7   }
    8};


    Subject: RE: Run 2 functions in one gadget
    Replied by: Jonathan Mendez on 23-12-2013 10:47:14 AM
    Already tried it and still not working.
    the error it gives me is to want to run a function of the second gadget.
    tells me that you have not initialized the init function of the second gadget.

    in that segment does not give me error but when I run the functions must be loaded in the init.

    Subject: RE: Run 2 functions in one gadget
    Replied by: David Lender on 23-12-2013 12:29:32 PM
    Are you trying to create an instance of the User object in each init?  The finesse javascript library only supports 1 instance of a User object per gadget.


     

    Subject: RE: Run 2 functions in one gadget
    Replied by: Jonathan Mendez on 23-12-2013 03:11:06 PM
    Thank you very much.
    I could say a way to run two gadgets (js) each with its user instance, in one as master, either iframe or otherwise.

    Subject: RE: Run 2 functions in one gadget
    Replied by: Juan Othmaro Menjivar on 24-12-2013 09:03:42 AM
    Good day David, thank you for assisting us.
    I am also working with Jonathan on developing these Gadgets.

    Our main issue is that we need to develop an agent desktop interface with at least two gadgets loading at the same time, for this approach we created one main gadget that load all rest. We've also splitted each gadget in separate JS files and those files need to have access to the User object or any other that can only be initialized once.  
    Finally we’d kindly appreciate if you could let us know what would be your recommendation in order to initialize gadgets and have those instances (e.g. User) available inside theirs private functions?

    Thank you,

    Juan

    Subject: RE: Run 2 functions in one gadget
    Replied by: Juan Othmaro Menjivar on 30-12-2013 09:22:52 AM
    Hello Joseph/David, do you have any comments about this?

    Subject: RE: Run 2 functions in one gadget
    Replied by: Joseph Horvath on 30-12-2013 07:37:34 PM
    I don't know if you are trying to write a Gadget that manages two different Users, or the same User in two different JavaScript files.

    If you are going for a Gadget that manages two different Users, then you most likely need custom code.

    The same User in two different JavaScript files should not be an issue, however, I'll try some prototype code to see if I get into trouble with it.

    Subject: RE: Run 2 functions in one gadget
    Replied by: Juan Othmaro Menjivar on 31-12-2013 08:07:10 AM
    Second option is the one it's the same user, same agent desktop loading two different gadgets, we coded the init() call back into both gadgets, both inits are referencing the user object, but in the second gadget it's saying that user object was not loaded.

    Our architecture is:

    CONTAINER / MAIN GADGET
      GADGET 1 << this is a fixed gadget
      GADGET 2 << this gadget is floating in the browser window and it placed right next to Gadget 1

    The only way we could make the floating feature is initializing GADGET 1 AND 2  from the CONTAINER, no iFrame since this is not able to access the Finesse instances in the desktop.

    initializegadgets.HubSettings.onConnect = function () {                 
       finesse.modules.file1.init ();                 
       finesse.modules.file2.init ();             
    };





    Subject: RE: Run 2 functions in one gadget
    Replied by: Joseph Horvath on 31-12-2013 10:15:02 AM
    I built a small prototype to see what happens when sharing a Finesse object between two JavaScript modules. I also expreienced issues when trying to do so.

    Have you tried to solve this problem by using two seperate Gadgets and communicating between them using publish/subscribe and the Gadget Hub?

    Subject: RE: Run 2 functions in one gadget
    Replied by: Joseph Horvath on 02-01-2014 10:37:23 AM
    I seem to be able to deal correctly with this issue by creating a "shared" User object and initializing the second file in the "handleUserLoad" function. Note that the Gadget's XML file only initializes the first file. Here is a condensed sample:

    File 1:
     1var finesse = finesse || {};
     2finesse.gadget = finesse.gadget || {};
     3finesse.container = finesse.container || {};
     4
     5// Configuration for the gadget
     6finesse.gadget.Config = (function () {
     7       var _prefs = new gadgets.Prefs();
     8       return {
     9                authorization: _prefs.getString ("authorization"),
    10                host: _prefs.getString ("host"),
    11                restHost: "localhost"
    12       };
    13}());
    14
    15/** @namespace */
    16finesse.modules = finesse.modules || {};
    17
    18finesse.modules.user = null;
    19
    20finesse.modules.File1 = (function ($) {
    21
    22    /**
    23     * Handler for the onLoad of a User object.  This occurs when the User object is initially read
    24     * from the Finesse server.  Any once only initialization should be done within this function.
    25     */
    26var handleUserLoad = function (userEvent) {
    27    finesse.modules.user = userEvent;
    28    console.log ("User state: " + finesse.modules.user.getState ());
    29    finesse.modules.File2.init ();
    30};
    31
    32    /**
    33     *  Handler for all User updates
    34     */
    35var handleUserChange = function(userEvent) {
    36        finesse.modules.user = userEvent;
    37};
    38
    39/** @scope finesse.modules.File1 */
    40return {
    41    /**
    42     * Performs all initialization for this gadget
    43     */
    44  init : function () {
    45            var prefs = new gadgets.Prefs ();
    46            var id = prefs.getString ("id");
    47
    48            gadgets.window.adjustHeight ();
    49
    50            // Initiate the ClientServices and load the user object.  ClientServices are
    51            // initialized with a reference to the current configuration.
    52            finesse.clientservices.ClientServices.init (finesse.gadget.Config);
    53
    54            finesse.modules.user = new finesse.restservices.User (
    55                {
    56                  id: id,
    57                        onLoad : handleUserLoad,
    58                        onChange : handleUserChange
    59                        }
    60                );
    61    }
    62};
    63}(jQuery));


    File 2:
     1var finesse = finesse || {};
     2finesse.gadget = finesse.gadget || {};
     3finesse.container = finesse.container || {};
     4
     5finesse.modules = finesse.modules || {};
     6
     7finesse.modules.File2 = (function ($) {
     8
     9/** @scope finesse.modules.File2 */
    10return {
    11    /**
    12     * Performs all initialization for this gadget
    13     */
    14  init : function () {
    15            console.log ("User state: " + finesse.modules.user.getState ());
    16    }
    17};
    18}(jQuery));


    Subject: RE: Run 2 functions in one gadget
    Replied by: Juan Othmaro Menjivar on 02-01-2014 08:03:47 AM
    Hello Joseph,

    Actually we started coding this as separate gadgets because our Gadget 2 has to move along when scrolling up/down in the browser's window, so we splited both gadgets into iframes but noticed they couldn't reach the Finesse object instance in the Agent desktop. So then we decided to try it out by defining one container gagdet wich would call the init() methods of the other 2 gadgets .

    initializegadgets.HubSettings.onConnect = function () {                 
       finesse.modules.file1.init ();                 
       finesse.modules.file2.init ();             
    };

    Now our problem is on the second call of the init() method because all derived functions referencing its objects says they can't find any reference created.

    Subject: RE: Run 2 functions in one gadget
    Replied by: Juan Othmaro Menjivar on 02-01-2014 10:49:50 AM
    Got it, we think this solves our issue.

    Thank you for your assistance.