2

PubNub works by calling callback functions you specify like this: http://www.pubnub.com/account-javascript-api-include

How to integrate this properly with the Qooxdoo JS framework? Qooxdoo application looks like this: http://demo.qooxdoo.org/current/playground/#Hello%20World-ria

How do you load the external JS library and make the global "PUBNUB" available?

Hristo Hristov
  • 4,021
  • 4
  • 25
  • 37

1 Answers1

4

You could easily use the add-script config key, to load the pubnub-*.min.js before your app. Then add the PUBNUB.subscribe() call anywhere in your qooxdoo code where it suits you, e.g. in the main method of your main class or in an event handler of a GUI element like a button.

EDIT:

To add more details:

  • You add the add-script key in config.json, in the "jobs" section.
  • As you need the script in both the source and the build version, you should add it to the source-script and the build-script jobs, or you create a separate job for the key and extend source-script and build-script with it (that would I do).
  • The warning about the job shadowing is only to alert people that inadvertently use a pre-defined job name for self-defined job. But here this is exactly what you want, and you can silence the warning with the config-warnings key if you wish. But this doesn't affect the built app.
  • As for the definedness of PUBNUB you might run into a timing issue where your code using PUBNUB is already executed when the pubnub script file has not finished loading. In your running app, first check in the command line (e.g. Firebug or Chrome Developer Tools) if the PUBNUB symbol is known. If so, the loading was successful. In that case you might want to delay access to the PUBNUB symbol in your code, e.g. by placing it in an execute listener of a button.

Here is a fragment of the possible config.json entries:

...
"jobs" : {
   "add-pubnub" : {
       "add-script" : [
         {
           "uri": "http://cdn.pubnub.com/pubnub-3.3.min.js"
         }
       ]
   },
   "source-script" : {
       "extend" : ["add-pubnub"]
   },
   "build-script" : {
       "extend" : ["add-pubnub"]
   }
...
ThomasH
  • 22,276
  • 13
  • 61
  • 62
  • Where do I have to add this config key? I have tried in "config.json", section "jobs", "source-script", but it gives a warning that the job is shadowed and later that used PUBNUB global is not defined. – Hristo Hristov Sep 16 '12 at 16:13
  • I am doing everything as described, but the warning is at build time: " - Warning: Hint: Unknown global symbol referenced: PUBNUB (custom.Application:72) " and later while running, in the javascript console: "Uncaught ReferenceError: PUBNUB is not defined " – Hristo Hristov Sep 18 '12 at 18:52
  • For some reason loading of http://cdn.pubnub.com/pubnub-3.1.min.js is not successful by following these instructions... – Hristo Hristov Sep 18 '12 at 19:25
  • We recently released the 3.3 version of our code, available at http://cdn.pubnub.com/pubnub-3.3.min.js. Let me see if I can get this to work using our latest and greatest. – Geremy Sep 18 '12 at 19:31
  • I am not sure it is because of pubnub, it is more likely to me that the script is not loaded for some reason by the "add-script" key. – Hristo Hristov Sep 18 '12 at 19:40
  • @HristoHristov For me it's working nicely, using a qooxdoo skeleton, for both pubnub-3.1 and pubnub-3.3. Mind though, that I had to fix the syntax of the ``add-script`` key in my answer. I just used ``PUBNUB.now()`` in my "main" method for testing, and both source and build version worked like a charm, no loading issues. – ThomasH Sep 18 '12 at 20:11
  • @ThomasH This worked for me too, the problem is that when I run "./generate.py source-all" PUBNUB is undefined at runtime, with "./generate.py source" everything is fine. Do you have an idea why? – Hristo Hristov Sep 20 '12 at 18:38
  • ... because source-script-all is not extended the same way :) – Hristo Hristov Sep 20 '12 at 18:47