Permalink
Browse files

Allow callback after multiple zones are loaded

- Add a `done` var to keep track of when all zones are done loading,
then invoke callback.
- Add unit test
  • Loading branch information...
1 parent 3131489 commit daa79253d6f76314fc9f10e35f02c05847b88764 @longlho longlho committed Jun 8, 2012
View
@@ -0,0 +1,37 @@
+var TestUtils = require('./test-utils')
+, parseISO = TestUtils.parseISO
+, date = require('../src/date');
+
+describe('TimezoneJS', function () {
+ it('should async preload everything correctly', function () {
+
+ var i = 0
+ , timezoneJS
+ , sampleTz;
+ runs(function () {
+ timezoneJS = TestUtils.getTimezoneJS({
+ loadingScheme: date.timezone.loadingSchemes.PRELOAD_ALL,
+ async: true,
+ callback: function () {
+ //Make sure more than 1 zone is loaded
+ for (var k in timezoneJS.timezone.loadedZones) {
+ i++;
+ }
+ sampleTz = timezoneJS.timezone.getTzInfo(new Date(), 'Asia/Bangkok');
+ }
+ });
+ });
+
+ waitsFor(function () {
+ return i > 0;
+ }, 'zones should be loaded', 100);
+ runs(function () {
+ expect(timezoneJS.timezone.loadingScheme).toEqual(date.timezone.loadingSchemes.PRELOAD_ALL);
+
+ expect(i).toEqual(timezoneJS.timezone.zoneFiles.length);
+ expect(sampleTz).toBeDefined();
+ expect(sampleTz.tzAbbr).toEqual('ICT');
+
+ });
+ });
+});
File renamed without changes.
File renamed without changes.
View
@@ -414,6 +414,7 @@
return !opts.async
? _this.parseZones(_this.transport({ url : url, async : false }))
: _this.transport({
+ async: true,
url : url,
success : function (str) {
if (_this.parseZones(str) && typeof opts.callback === 'function') {
@@ -722,19 +723,25 @@
this.rules = {};
this.init = function (o) {
- var opts = { async: true };
- var def = this.defaultZoneFile = this.loadingScheme === this.loadingSchemes.PRELOAD_ALL
- ? this.zoneFiles
- : 'northamerica';
+ var opts = { async: true }
+ , def = this.defaultZoneFile = this.loadingScheme === this.loadingSchemes.PRELOAD_ALL
+ ? this.zoneFiles
+ : 'northamerica'
+ , done = 0
+ , callbackFn;
// Override default with any passed-in opts
for (var p in o) {
opts[p] = o[p];
}
if (typeof def === 'string') {
return this.loadZoneFile(def, opts);
}
- if (opts.callback) {
- throw new Error('Async load with callback is not supported for multiple default zonefiles.');
+ //Wraps callback function in another one that makes
+ //sure all files have been loaded.
+ callbackFn = opts.callback;
+ opts.callback = function () {
+ done++;
+ (done === def.length) && typeof callbackFn === 'function' && callbackFn();
}
for (var i = 0; i < def.length; i++) {
this.loadZoneFile(def[i], opts);
@@ -825,6 +832,7 @@
}
}
}
+ return true;
};
// Expose transport mechanism and allow overwrite
this.transport = _transport;

0 comments on commit daa7925

Please sign in to comment.