hub globals and arrays

30 views
Skip to first unread message

trippah

unread,
Sep 12, 2014, 11:00:59 AM9/12/14
to kern...@googlegroups.com
Hi

I have a hub with several global variables.

Kernel.hub.define('main', {
gpsStarted: false,
gpsBusy: false,
        gpsDevices: []
});

I can access these vars from Kernel with:
         Kernel.hub.gpsStarted
but the array "gpsDevices" is not being recognized.

What am I doing wrong? 
Message has been deleted

trippah

unread,
Sep 12, 2014, 11:04:14 AM9/12/14
to kern...@googlegroups.com
I am accessing the array like:
           var myGPS = Kernel.hub.gpsDevices[1];
or
           Kernel.hub.gpsDevices.push('Garmin');

alindsay55661

unread,
Sep 12, 2014, 11:24:55 AM9/12/14
to kern...@googlegroups.com
trippah,

Kernel.hub is a namespace, not a hub instance. To gain access to a hub instance you call get and pass it the instance id.

var hub = Kernel.hub.get('main');
var myGPS = hub.gpsDevices[1];
hub.gpsDevices.push('Garmin');

I can access these vars from Kernel with:
         Kernel.hub.gpsStarted

This would only be true if you are adding gpsStarted to the Kernel.hub namespace, which you should not do. Otherwise gpsStarted would be available from your hub instance like everything else.

Alan

trippah

unread,
Sep 13, 2014, 2:12:23 AM9/13/14
to kern...@googlegroups.com
Thanks Alan get() is working. 

On Saturday, September 13, 2014 1:24:55 AM UTC+10, alindsay55661 wrote:
This would only be true if you are adding gpsStarted to the Kernel.hub namespace, which you should not do. Otherwise gpsStarted would be available from your hub instance like everything else.


Sorry for my ignorance but what syntax difference is there adding var to kernel.hub namespace and hub instance?
Currently I want to keep a set of "global" vars which is accessible from kernel extension/hub/module so i define them in my hub as so:

Kernel.hub.define('main', {
gpsStarted: false,
gpsBusy: false,
});
 
 and now I access them through the get() method as you have stated. Is this the correct way?

alan lindsay

unread,
Sep 13, 2014, 2:29:05 AM9/13/14
to kern...@googlegroups.com
Yes, this is the correct way. You really don't need to (and shouldn't) add anything directly to Kernel.hub unless you are expanding the architecture. Instead put your "globals" in the hub definition like you are doing above. You can then access these variables from any module like so:

Kernel.module.define('MyModule', {
  init: function() {

    if (this.hub.gpsStarted) {
      // Do something
    }
  }
});

If you need to access these variables from other hubs or from Kernel itself you would do this:

Kernel.extend(Kernel, {

  myExtension: {

    foo: function() {
      var hub = Kernel.hub.get('main');

      if (hub.gpsStarted) {
        // Do something
      }
    }

  }

});

Hope this helps.

Alan


--
You received this message because you are subscribed to the Google Groups "Kernel.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kerneljs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages