4

I am working on a legacy application and all JS seems mystrious to me.
Here is the few mysterious lines which are loaded before all and I don't have any idea what they are doing.

var i2b2 = {sdx:{TypeControllers:{},Master:{_sysData:{}}},events:{},hive:{cfg:{},helpers:{},base_classes:{}},h:{}};  
if (undefined==i2b2.hive) { i2b2.hive = {}; }     
i2b2.hive.tempCellsList = [
        { code: "PM",
          forceLoading: true 
        },
        { code: "ONT"   },
        { code: "CRC"   },
        { code: "WORK"},
        { code: "SHRINE"},
        { code: "PLUGINMGR",
           forceLoading: true,
           forceConfigMsg: { params: [] }
        }
    ];

There are many more var and if statements but they are doing same thing with different variables.
Please help me to solve this mystery.

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • Oh dear… type coercion comparison with `undefined`. Yaiks. If you end up reusing this code, do yourself a favor and replace it with `if (typeof i2b2.hive === 'undefined') …` :) – Flambino Aug 11 '11 at 09:35
  • 2
    Why the close votes for "too localized"? IMHO this is a valid question, although the poster should describe better what exactly (s)he doesn't understand. – RoToRa Aug 11 '11 at 09:39
  • Possible source: [i2b2_loader.js](http://informatics.kumc.edu/work/browser/i2b2-webclient/webclient/js-i2b2/i2b2_loader.js?desc=1) – Shawn Chin Aug 11 '11 at 09:45
  • @Flambino: I just had a glimpse of it and have nothing done by myself. Still will keep this in mind – Ajinkya Aug 11 '11 at 09:46
  • Feel free to add the "i2b2" tag to this question – JustBeingHelpful Jul 12 '12 at 19:27

4 Answers4

4

The first line initialises i2b2 using nested object literals.

var obj = {}; is a shorter way of writing var obj = new Object();

A simple object literal will be

var simpleObject = {
    property1: "Hello",
    property2: "MmmMMm",
    property3: ["mmm", 2, 3, 6, "kkk"],
    method1: function() {
        alert("my method")
    }
};

A nested one will be

var rectangle = {
    upperLeft: {
        x: 2,
        y: 2
    },
    lowerRight: {
        x: 4,
        y: 4
    }
};

Yours is a classic.

var i2b2 = {
    sdx: {
        TypeControllers: {},
        Master: {
            _sysData: {}
        }
    },
    events: {},
    hive: {
        cfg: {},
        helpers: {},
        base_classes: {}
    },
    h: {}
};

The second line should be IMHO

i2b2.hive = i2b2.hive || {};

This just says that if hive is undefined create a new object.

The last lines create a property tempCellsList to the object hive. ( Please note that hive in turn is a property of i2b2 ) Lastly a new array of objects are added to the property tempCellsList

naveen
  • 53,448
  • 46
  • 161
  • 251
  • Thanks for such a detail explanation. So 'sdx:{ TypeControllers:{},Master: {_sysData: {}}}' just mean var `i2b2` and property `sdx` which have p[roperties `TypeControllers:{}` and `Master ` ? – Ajinkya Aug 11 '11 at 09:44
  • yes. thats it. put your whole code here so that you can read it properly. http://jsbeautifier.org/ – naveen Aug 11 '11 at 09:48
  • One thing I observed. On first line we are defining `i2b2.hive` var and on second we are checking if it is `undefined`. Does it make sense ? – Ajinkya Aug 11 '11 at 09:49
  • no it doesn't and also you could have put the last lines on the first line itself as another property inside hive. – naveen Aug 11 '11 at 09:52
1

This javascript code creates a variable called ib2b that has a number of properties: sdx, events, hive, etc. Those properties hold more composite objects, which are constructed below.

The idea is that this global object can be referenced from other javascript code and it stores global configuration for the client-side application.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
1

I'm not quite sure, what exactly you don't understand. There are two "strange" points about the code above, which I'll try to explain, but if that's not enough you will need to describe better what you don't understand:

  1. The code checks is i2b2.hive is is undefined and set it as an empty object, if it is. Since the property is obviously set in the previous line, my guess is that this code is generated dynamically and some of the logic (such as this check) is defined in the JavaScript code even if it could (should?) be the the server side code.

  2. undefined==i2b2.hive is a bad/wrong way to test "undefinedness", because undefined is not a reserved word in JavaScript.This just works, because undefined is just a variable that - by chance - happens to be undefined. Instead one should use if (typeof i2b2.hive == "undefined") ... or just if (i2b2.hive) ....

RoToRa
  • 37,635
  • 12
  • 69
  • 105
0

It seems like it's setting a few parameters in that i2b2 object. It doesn't "do" anything in itself, but it seems to set a few basic configuration settings for further execution. Try and look for similar occurrences in the code further below.

E.g it sets i2b2.hive.tempCellList[5].forceLoading to true. Later on the application probably has if-conditions, such as

for(var i in i2b2.hive.tempCellList)
{
    if(i2b2.hive.tempCellList[i].forceLoading === true)
    {
        // do something...
    }
}
kgilden
  • 10,336
  • 3
  • 50
  • 48