From 0f17b7f7364ab9e7f7ac990450cd4f9dfbad96dd Mon Sep 17 00:00:00 2001 From: Asaf David Date: Sun, 18 Jan 2015 15:21:47 +0200 Subject: [PATCH 1/3] Unbind events --- .gitignore | 1 + scripts/angular-parallax.js | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/scripts/angular-parallax.js b/scripts/angular-parallax.js index 6ec39e8..83be213 100644 --- a/scripts/angular-parallax.js +++ b/scripts/angular-parallax.js @@ -25,6 +25,11 @@ angular.module('angular-parallax', [ angular.element($window).bind("scroll", setPosition); angular.element($window).bind("touchmove", setPosition); + + $scope.$on('$destroy', function() { + angular.element($window).unbind("scroll", setPosition); + angular.element($window).unbind("touchmove", setPosition); + }); } // link function }; }]).directive('parallaxBackground', ['$window', function($window) { @@ -50,6 +55,12 @@ angular.module('angular-parallax', [ angular.element($window).bind("scroll", setPosition); angular.element($window).bind("touchmove", setPosition); + + // Unbind events on + $scope.$on('$destroy', function() { + angular.element($window).unbind("scroll", setPosition); + angular.element($window).unbind("touchmove", setPosition); + }); } // link function }; }]); From c73d4fcf8c2f0996a8e6cc0b9e8d15d585f9b4a4 Mon Sep 17 00:00:00 2001 From: Asaf David Date: Sun, 18 Jan 2015 15:51:11 +0200 Subject: [PATCH 2/3] Handle attribute removal --- scripts/angular-parallax.js | 44 +++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/scripts/angular-parallax.js b/scripts/angular-parallax.js index 83be213..e57af57 100644 --- a/scripts/angular-parallax.js +++ b/scripts/angular-parallax.js @@ -7,7 +7,7 @@ angular.module('angular-parallax', [ scope: { parallaxRatio: '@', parallaxVerticalOffset: '@', - parallaxHorizontalOffset: '@', + parallaxHorizontalOffset: '@' }, link: function($scope, elem, $attrs) { var setPosition = function () { @@ -23,13 +23,25 @@ angular.module('angular-parallax', [ setPosition(); - angular.element($window).bind("scroll", setPosition); - angular.element($window).bind("touchmove", setPosition); - - $scope.$on('$destroy', function() { + // Handle events + var bind = function() { + angular.element($window).bind("scroll", setPosition); + angular.element($window).bind("touchmove", setPosition); + }; + var unbind = function() { angular.element($window).unbind("scroll", setPosition); angular.element($window).unbind("touchmove", setPosition); + }; + $scope.$on('$destroy', function() { + unbind(); }); + attrs.$observe('parallaxBackground', function(val) { + if (val) { + bind() + } else { + unbind(); + } + }) } // link function }; }]).directive('parallaxBackground', ['$window', function($window) { @@ -53,14 +65,28 @@ angular.module('angular-parallax', [ $scope.$apply(); }); - angular.element($window).bind("scroll", setPosition); - angular.element($window).bind("touchmove", setPosition); + // Handle events + var bind = function() { + angular.element($window).bind("scroll", setPosition); + angular.element($window).bind("touchmove", setPosition); + }; // Unbind events on - $scope.$on('$destroy', function() { + var unbind = function() { + console.log('$destroy'); angular.element($window).unbind("scroll", setPosition); angular.element($window).unbind("touchmove", setPosition); - }); + } + $scope.$on('$destroy', function() { + unbind(); + }); + attrs.$observe('parallaxBackground', function(val) { + if (val) { + bind(); + } else { + unbind(); + } + }) } // link function }; }]); From e03535047ea48c97974edd1641094b83e5f9050a Mon Sep 17 00:00:00 2001 From: Asaf David Date: Sun, 15 Feb 2015 12:25:55 +0200 Subject: [PATCH 3/3] Fix issue --- scripts/angular-parallax.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/angular-parallax.js b/scripts/angular-parallax.js index e57af57..875d7eb 100644 --- a/scripts/angular-parallax.js +++ b/scripts/angular-parallax.js @@ -35,7 +35,7 @@ angular.module('angular-parallax', [ $scope.$on('$destroy', function() { unbind(); }); - attrs.$observe('parallaxBackground', function(val) { + attrs.$observe('parallax', function(val) { if (val) { bind() } else { @@ -73,7 +73,6 @@ angular.module('angular-parallax', [ // Unbind events on var unbind = function() { - console.log('$destroy'); angular.element($window).unbind("scroll", setPosition); angular.element($window).unbind("touchmove", setPosition); }