Skip to content

Commit

Permalink
fix: don't reject file load with custom file loaders such as requirejs
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
ocombe committed Jul 14, 2014
1 parent 9550bce commit 91ed522
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 82 deletions.
3 changes: 2 additions & 1 deletion examples/example2/js/testmodule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ angular.module('test', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
loadedModules: ['test'],
asyncLoader: requirejs
jsLoader: requirejs,
debug: true
});
}])
.controller('mainController', ['$scope', '$ocLazyLoad', function($scope, $ocLazyLoad) {
Expand Down
171 changes: 90 additions & 81 deletions src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,90 +94,99 @@
return deferred.promise;
}

/**
* jsLoader function
* @type Function
* @param paths array list of js files to load
* @param callback to call when everything is loaded. We use a callback and not a promise
* because the user can overwrite jsLoader and it will probably not use promises :(
*/
jsLoader = jsLoader || function(paths, callback) {
var promises = [];
angular.forEach(paths, function loading(path) {
promises.push(buildElement('js', path));
});
$q.all(promises).then(function success() {
callback();
}, function error(err) {
callback(err);
});
if(angular.isUndefined(jsLoader)) {
/**
* jsLoader function
* @type Function
* @param paths array list of js files to load
* @param callback to call when everything is loaded. We use a callback and not a promise
* because the user can overwrite jsLoader and it will probably not use promises :(
*/
jsLoader = function(paths, callback) {
var promises = [];
angular.forEach(paths, function loading(path) {
promises.push(buildElement('js', path));
});
$q.all(promises).then(function success() {
callback();
}, function error(err) {
callback(err);
});
}
jsLoader.ocLazyLoadLoader = true;
}

/**
* cssLoader function
* @type Function
* @param paths array list of css files to load
* @param callback to call when everything is loaded. We use a callback and not a promise
* because the user can overwrite cssLoader and it will probably not use promises :(
*/
cssLoader = cssLoader || function(paths, callback) {
var promises = [];
angular.forEach(paths, function loading(path) {
promises.push(buildElement('css', path));
});
$q.all(promises).then(function success() {
callback();
}, function error(err) {
callback(err);
});
if(angular.isUndefined(cssLoader)) {
/**
* cssLoader function
* @type Function
* @param paths array list of css files to load
* @param callback to call when everything is loaded. We use a callback and not a promise
* because the user can overwrite cssLoader and it will probably not use promises :(
*/
cssLoader = function(paths, callback) {
var promises = [];
angular.forEach(paths, function loading(path) {
promises.push(buildElement('css', path));
});
$q.all(promises).then(function success() {
callback();
}, function error(err) {
callback(err);
});
}
cssLoader.ocLazyLoadLoader = true;
}

/**
* templatesLoader function
* @type Function
* @param paths array list of css files to load
* @param params object config parameters for $http
* @param callback to call when everything is loaded. We use a callback and not a promise
* because the user can overwrite templatesLoader and it will probably not use promises :(
*/
templatesLoader = templatesLoader || function(paths, params, callback) {
if(angular.isString(paths)) {
paths = [paths];
}
if(angular.isFunction(params)) {
callback = params;
params = {};
}
if(angular.isUndefined(params)) {
params = {};
}
var promises = [];
angular.forEach(paths, function(url) {
var deferred = $q.defer();
promises.push(deferred.promise);
if(templates.indexOf(url) === -1 || (angular.isDefined(params) && params.cache === false)) {
$http.get(url, params).success(function(data) {
angular.forEach(angular.element(data), function(node) {
if(node.nodeName === 'SCRIPT' && node.type === 'text/ng-template') {
$templateCache.put(node.id, node.innerHTML);
}
if(angular.isUndefined(templatesLoader)) {
/**
* templatesLoader function
* @type Function
* @param paths array list of css files to load
* @param params object config parameters for $http
* @param callback to call when everything is loaded. We use a callback and not a promise
* because the user can overwrite templatesLoader and it will probably not use promises :(
*/
templatesLoader = function(paths, params, callback) {
if(angular.isString(paths)) {
paths = [paths];
}
if(angular.isFunction(params)) {
callback = params;
params = {};
}
if(angular.isUndefined(params)) {
params = {};
}
var promises = [];
angular.forEach(paths, function(url) {
var deferred = $q.defer();
promises.push(deferred.promise);
if(templates.indexOf(url) === -1 || (angular.isDefined(params) && params.cache === false)) {
$http.get(url, params).success(function(data) {
angular.forEach(angular.element(data), function(node) {
if(node.nodeName === 'SCRIPT' && node.type === 'text/ng-template') {
$templateCache.put(node.id, node.innerHTML);
}
});
templates.push(url);
deferred.resolve();
}).error(function(data) {
var err = 'Error load template "' + url + '": ' + data;
$log.error(err);
deferred.reject(new Error(err));
});
templates.push(url);
} else {
deferred.resolve();
}).error(function(data) {
var err = 'Error load template "' + url + '": ' + data;
$log.error(err);
deferred.reject(new Error(err));
});
} else {
deferred.resolve();
}
});
return $q.all(promises).then(function success() {
callback();
}, function error(err) {
callback(err);
});;
}
});
return $q.all(promises).then(function success() {
callback();
}, function error(err) {
callback(err);
});
}
templatesLoader.ocLazyLoadLoader = true;
}

var filesLoader = function(paths, params) {
Expand All @@ -199,7 +208,7 @@
if(cssFiles.length > 0) {
var cssDeferred = $q.defer();
cssLoader(cssFiles, function(err) {
if(angular.isDefined(err)) {
if(angular.isDefined(err) && cssLoader.hasOwnProperty('ocLazyLoadLoader')) {
$log.error(err);
cssDeferred.reject(err);
} else {
Expand All @@ -212,7 +221,7 @@
if(templatesFiles.length > 0) {
var templatesDeferred = $q.defer();
templatesLoader(templatesFiles, params, function(err) {
if(angular.isDefined(err)) {
if(angular.isDefined(err) && templatesLoader.hasOwnProperty('ocLazyLoadLoader')) {
$log.error(err);
templatesDeferred.reject(err);
} else {
Expand All @@ -225,7 +234,7 @@
if(jsFiles.length > 0) {
var jsDeferred = $q.defer();
jsLoader(jsFiles, function(err) {
if(angular.isDefined(err)) {
if(angular.isDefined(err) && jsLoader.hasOwnProperty('ocLazyLoadLoader')) {
$log.error(err);
jsDeferred.reject(err);
} else {
Expand Down

0 comments on commit 91ed522

Please sign in to comment.