Skip to content

Commit

Permalink
feat: new parameter serie to load files in serie
Browse files Browse the repository at this point in the history
Closes #47
Fixes #86
  • Loading branch information
ocombe committed Nov 9, 2014
1 parent 26a64a3 commit 4ae7a3f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ $ocLazyLoad.load(
);
```

The existing parameters that you can use are `cache` and `reconfig`.
The existing parameters that you can use are `cache`, `reconfig`, `rerun` and `serie`.
The parameter `cache: false` works for all native loaders (**all requests are cached by default**):

```js
Expand Down Expand Up @@ -173,6 +173,18 @@ $ocLazyLoad.load({
});
```

By default the native loaders will load your files in parallel. If you need to load your files in serie, you can use `serie: true`:
```js
$ocLazyLoad.load({
name: 'mgcrea.ngStrap',
serie: true,
files: [
'bower_components/angular-strap/dist/angular-strap.js',
'bower_components/angular-strap/dist/angular-strap.tpl.js'
]
});
```

### Directive
The directive usage is very similar to the service. The main interest here is that the content will be included and compiled once your modules have been loaded.
This means that you can use directives that will be lazy loaded inside the oc-lazy-load directive.
Expand Down
22 changes: 18 additions & 4 deletions dist/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@

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

angular.forEach(params.files, function(path) {
var pushFile = function(path) {
cachePromise = filesCache.get(path);
if(angular.isUndefined(cachePromise) || params.cache === false) {
if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) {
Expand All @@ -270,7 +270,15 @@
} else if(cachePromise) {
promises.push(cachePromise);
}
});
}

if(params.serie) {
pushFile(params.files.shift());
} else {
angular.forEach(params.files, function(path) {
pushFile(path);
});
}

if(cssFiles.length > 0) {
var cssDeferred = $q.defer();
Expand Down Expand Up @@ -311,7 +319,13 @@
promises.push(jsDeferred.promise);
}

return $q.all(promises);
if(params.serie && params.files.length > 0) {
return $q.all(promises).then(function success() {
return filesLoader(config, params);
});
} else {
return $q.all(promises);
}
}

return {
Expand Down Expand Up @@ -656,7 +670,7 @@
return model($scope) || $attr.ocLazyLoad;
}, function(moduleName) {
if(angular.isDefined(moduleName)) {
$ocLazyLoad.load(moduleName).then(function(moduleConfig) {
$ocLazyLoad.load(moduleName).then(function success(moduleConfig) {
$animate.enter($compile(content)($scope), null, $element);
});
}
Expand Down
2 changes: 1 addition & 1 deletion dist/ocLazyLoad.min.js

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions examples/example1/js/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@

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

angular.forEach(params.files, function(path) {
var pushFile = function(path) {
cachePromise = filesCache.get(path);
if(angular.isUndefined(cachePromise) || params.cache === false) {
if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) {
Expand All @@ -270,7 +270,15 @@
} else if(cachePromise) {
promises.push(cachePromise);
}
});
}

if(params.serie) {
pushFile(params.files.shift());
} else {
angular.forEach(params.files, function(path) {
pushFile(path);
});
}

if(cssFiles.length > 0) {
var cssDeferred = $q.defer();
Expand Down Expand Up @@ -311,7 +319,13 @@
promises.push(jsDeferred.promise);
}

return $q.all(promises);
if(params.serie && params.files.length > 0) {
return $q.all(promises).then(function success() {
return filesLoader(config, params);
});
} else {
return $q.all(promises);
}
}

return {
Expand Down Expand Up @@ -656,7 +670,7 @@
return model($scope) || $attr.ocLazyLoad;
}, function(moduleName) {
if(angular.isDefined(moduleName)) {
$ocLazyLoad.load(moduleName).then(function(moduleConfig) {
$ocLazyLoad.load(moduleName).then(function success(moduleConfig) {
$animate.enter($compile(content)($scope), null, $element);
});
}
Expand Down
22 changes: 18 additions & 4 deletions examples/example2/js/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@

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

angular.forEach(params.files, function(path) {
var pushFile = function(path) {
cachePromise = filesCache.get(path);
if(angular.isUndefined(cachePromise) || params.cache === false) {
if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) {
Expand All @@ -270,7 +270,15 @@
} else if(cachePromise) {
promises.push(cachePromise);
}
});
}

if(params.serie) {
pushFile(params.files.shift());
} else {
angular.forEach(params.files, function(path) {
pushFile(path);
});
}

if(cssFiles.length > 0) {
var cssDeferred = $q.defer();
Expand Down Expand Up @@ -311,7 +319,13 @@
promises.push(jsDeferred.promise);
}

return $q.all(promises);
if(params.serie && params.files.length > 0) {
return $q.all(promises).then(function success() {
return filesLoader(config, params);
});
} else {
return $q.all(promises);
}
}

return {
Expand Down Expand Up @@ -656,7 +670,7 @@
return model($scope) || $attr.ocLazyLoad;
}, function(moduleName) {
if(angular.isDefined(moduleName)) {
$ocLazyLoad.load(moduleName).then(function(moduleConfig) {
$ocLazyLoad.load(moduleName).then(function success(moduleConfig) {
$animate.enter($compile(content)($scope), null, $element);
});
}
Expand Down
22 changes: 18 additions & 4 deletions src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@

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

angular.forEach(params.files, function(path) {
var pushFile = function(path) {
cachePromise = filesCache.get(path);
if(angular.isUndefined(cachePromise) || params.cache === false) {
if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) {
Expand All @@ -263,7 +263,15 @@
} else if(cachePromise) {
promises.push(cachePromise);
}
});
}

if(params.serie) {
pushFile(params.files.shift());
} else {
angular.forEach(params.files, function(path) {
pushFile(path);
});
}

if(cssFiles.length > 0) {
var cssDeferred = $q.defer();
Expand Down Expand Up @@ -304,7 +312,13 @@
promises.push(jsDeferred.promise);
}

return $q.all(promises);
if(params.serie && params.files.length > 0) {
return $q.all(promises).then(function success() {
return filesLoader(config, params);
});
} else {
return $q.all(promises);
}
}

return {
Expand Down Expand Up @@ -649,7 +663,7 @@
return model($scope) || $attr.ocLazyLoad;
}, function(moduleName) {
if(angular.isDefined(moduleName)) {
$ocLazyLoad.load(moduleName).then(function(moduleConfig) {
$ocLazyLoad.load(moduleName).then(function success(moduleConfig) {
$animate.enter($compile(content)($scope), null, $element);
});
}
Expand Down

0 comments on commit 4ae7a3f

Please sign in to comment.