Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for two issues found using nested views in ui-router #44

Merged
merged 3 commits into from
Sep 2, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
}
};

// Store the promise early so the file load can be detected by other parallel lazy loads
// (ie: multiple routes on one page) a 'true' value isn't sufficient
// as it causes false positive load results.
if(angular.isUndefined(filesCache.get(path))) {
filesCache.put(path, deferred.promise);
}

// Switch in case more content types are added later
switch(type) {
case 'css':
Expand All @@ -87,9 +94,6 @@
if((el['readyState'] && !(/^c|loade/.test(el['readyState']))) || loaded) return;
el.onload = el['onreadystatechange'] = null
loaded = 1;
if(angular.isUndefined(filesCache.get(path))) {
filesCache.put(path, true);
}
broadcast('ocLazyLoad.fileLoaded', path);
deferred.resolve();
}
Expand Down Expand Up @@ -194,20 +198,24 @@
var cssFiles = [],
templatesFiles = [],
jsFiles = [],
promises = [];
promises = [],
cachePromise = null;

angular.extend(params || {}, config);

angular.forEach(params.files, function(path) {
if(angular.isUndefined(filesCache.get(path)) || params.cache === false) {
cachePromise = filesCache.get(path);
if(angular.isUndefined(cachePromise) || params.cache === false) {
if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) {
cssFiles.push(path);
} else if(/\.(htm|html)[^\.]*$/.test(path) && templatesFiles.indexOf(path) === -1) {
templatesFiles.push(path);
} else if (jsFiles.indexOf(path) === -1) {
jsFiles.push(path);
}
}
} else if (cachePromise) {
promises.push(cachePromise);
}
});

if(cssFiles.length > 0) {
Expand Down Expand Up @@ -388,21 +396,23 @@
requireEntry = config;
}

// Check if this dependency has been loaded previously or is already in the moduleCache
if(moduleExists(requireEntry.name) || moduleCache.indexOf(requireEntry.name) !== -1) {
// Check if this dependency has been loaded previously
if(moduleExists(requireEntry.name)) {
if(typeof module !== 'string') {
// compare against the already loaded module to see if the new definition adds any new files
diff = requireEntry.files.filter(function (n) {
return self.getModuleConfig(requireEntry.name).files.indexOf(n) < 0
return self.getModuleConfig(requireEntry.name).files.indexOf(n) < 0;
});

// If the module was redefined, advise via the console
if (diff.length !== 0) {
$log.warn('Module "', moduleName, '" attempted to redefine configuration for dependency. "', requireEntry.name, '"\n Additional Files Loaded:', diff);
var c = angular.copy(requireEntry);
c.files = diff;
promisesList.push(filesLoader(c, params).then(function () {
return loadDependencies(requireEntry);
}));
}

// Push everything to the file loader, it will weed out the duplicates.
promisesList.push(filesLoader(requireEntry.files, params).then(function () {
return loadDependencies(requireEntry);
}));
}
return;
} else if(typeof requireEntry === 'object') {
Expand All @@ -426,7 +436,7 @@
if(requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) {
if(requireEntry.files) {
promisesList.push(filesLoader(requireEntry, params).then(function() {
return loadDependencies(requireEntry)
return loadDependencies(requireEntry);
}));
}
}
Expand Down