Skip to content

Commit

Permalink
Merge pull request #16 from redsoul/master
Browse files Browse the repository at this point in the history
placeholders were not correctly loading
  • Loading branch information
onemanclapping authored May 1, 2017
2 parents 8aa069b + e496496 commit 71e2e8e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
bower_components/
coverage/
.idea/
*.iml
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.2.1
### Bug Fixes
placeholders were not correctly loading with lazy-hide parameter on lazy-module directive

# 1.2.0
### Features
lazy-hide parameter on lazy-module directive
Expand Down
12 changes: 9 additions & 3 deletions dist/ng-lazy-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ angular.module('ngLazyRender', []);
angular.module('ngLazyRender').directive('lazyModule', ['$animate', '$compile', '$parse', '$q', '$templateCache', 'VisibilityService', function ($animate, $compile, $parse, $q, $templateCache, VisibilityService) {
'use strict';

var hidePlaceholder = function hidePlaceholder($transclude, scope, placeholderElem, moduleElem, finallyCb) {
var removePlaceholder = function removePlaceholder($transclude, scope, placeholderElem, moduleElem, finallyCb) {
// If the function is called after the scope is destroyed (more than once),
// we should do nothing.
if (scope === null) {
Expand Down Expand Up @@ -66,9 +66,15 @@ angular.module('ngLazyRender').directive('lazyModule', ['$animate', '$compile',
var watcher = void 0;

if ($attr.lazyHide) {
//load the placeholder
$animate.enter(el, $element.parent(), $element).then(function () {
$compile(el)(isolateScope);
});

//watch lazyHide attribute, when true remove the placeholder, show the module and remove the watch
watcher = $scope.$watch($attr.lazyHide, function (value) {
if (!!value) {
hidePlaceholder($transclude, isolateScope, el, $element, watcher);
removePlaceholder($transclude, isolateScope, el, $element, watcher);
}
});
} else {
Expand All @@ -77,7 +83,7 @@ angular.module('ngLazyRender').directive('lazyModule', ['$animate', '$compile',
// the actual transcluded content.
isolateScope.showModule = function () {
$scope.$applyAsync(function () {
hidePlaceholder($transclude, isolateScope, el, $element);
removePlaceholder($transclude, isolateScope, el, $element);
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-lazy-render",
"version": "1.2.0",
"version": "1.2.1",
"description": "A set of directives to postpone your angular application from rendering elements outside the viewport.",
"contributors": ["Andre Duarte <[email protected]>", "Joao Pereira <[email protected]>"],
"main": "./dist/ng-lazy-render.js",
Expand Down
6 changes: 6 additions & 0 deletions src/lazy-module-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ angular.module(`ngLazyRender`).directive(`lazyModule`, [
let watcher;

if ($attr.lazyHide) {
//load the placeholder
$animate.enter(el, $element.parent(), $element).then(() => {
$compile(el)(isolateScope)
})

//watch lazyHide attribute, when true remove the placeholder, show the module and remove the watch
watcher = $scope.$watch($attr.lazyHide, (value) => {
if (!!value) {
removePlaceholder($transclude, isolateScope, el, $element, watcher)
Expand Down
8 changes: 3 additions & 5 deletions tests/lazy-module-directive-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,15 @@ describe(`lazyModule directive`, () => {

// After compiling the directive we should no longer be able to see the content
expect(el.find('module').length).toBe(0)

// Also, we should now see the placeholder (article) and it should have its own scope
var lazyScope = el.find('placeholder').scope()
expect(lazyScope).not.toBe(initialScope)
// the placeholder should be visible
expect(el.find('placeholder').length).toBe(1)

$rootScope.hidePlaceholder = true
initialScope.$digest()
// Now the placeholder should not be visible anymore
expect(el.find('placeholder').length).toBe(0)

// And the module should be visible again
expect(el.find('module').length).not.toBe(0)
expect(el.find('module').length).toBe(1)
})
})

0 comments on commit 71e2e8e

Please sign in to comment.