From b29aeff35f062c5adc189729191c8e8802a48384 Mon Sep 17 00:00:00 2001 From: Adrian Ber Date: Sat, 21 Nov 2015 21:46:21 +0200 Subject: [PATCH] checklistBeforeChange --- checklist-model.js | 30 +++++++++++++++++++++++------- docs/blocks/event/ctrl.js | 12 ++++++++---- docs/blocks/event/test.js | 8 +++++++- docs/blocks/event/view.html | 2 +- docs/index.jade | 1 - index.html | 29 ++++++++++++++++++----------- readme.md | 17 +++++++++-------- 7 files changed, 66 insertions(+), 33 deletions(-) diff --git a/checklist-model.js b/checklist-model.js index 446f910..391d831 100644 --- a/checklist-model.js +++ b/checklist-model.js @@ -54,6 +54,7 @@ angular.module('checklist-model', []) var getter = $parse(checklistModel); var setter = getter.assign; var checklistChange = $parse(attrs.checklistChange); + var checklistBeforeChange = $parse(attrs.checklistBeforeChange); // value added to list var value = attrs.checklistValue ? $parse(attrs.checklistValue)(scope.$parent) : attrs.value; @@ -78,23 +79,38 @@ angular.module('checklist-model', []) if (newValue === oldValue) { return; } + + if (checklistBeforeChange && (checklistBeforeChange(scope) === false)) { + scope[attrs.ngModel] = contains(getter(scope.$parent), value, comparator); + return; + } + + setValueInChecklistModel(value, newValue); + + if (checklistChange) { + checklistChange(scope); + } + }); + + function setValueInChecklistModel(value, checked) { var current = getter(scope.$parent); if (angular.isFunction(setter)) { - if (newValue === true) { + if (checked === true) { setter(scope.$parent, add(current, value, comparator)); } else { setter(scope.$parent, remove(current, value, comparator)); } } + + } - if (checklistChange) { - checklistChange(scope); - } - }); - // declare one function to be used for both $watch functions function setChecked(newArr, oldArr) { - scope[attrs.ngModel] = contains(newArr, value, comparator); + if (checklistBeforeChange && (checklistBeforeChange(scope) === false)) { + setValueInChecklistModel(value, scope[attrs.ngModel]); + return; + } + scope[attrs.ngModel] = contains(newArr, value, comparator); } // watch original model change diff --git a/docs/blocks/event/ctrl.js b/docs/blocks/event/ctrl.js index 70be993..3333787 100644 --- a/docs/blocks/event/ctrl.js +++ b/docs/blocks/event/ctrl.js @@ -6,10 +6,14 @@ app.controller('Ctrl5', function($scope) { u: 'User' }; - $scope.testValue = 'Im not changed yet!'; - $scope.imChanged = function(){ - $scope.testValue = $scope.user.roles.join(','); - } + $scope.testValue = 'Im not changed yet!'; + $scope.imChanged = function(){ + $scope.testValue = $scope.user.roles.join(','); + } + $scope.shouldChange = function(key){ +console.log("should change " + key); + return key !== "g"; + } $scope.user = { roles: ['c'] diff --git a/docs/blocks/event/test.js b/docs/blocks/event/test.js index d594cfa..09f8fca 100644 --- a/docs/blocks/event/test.js +++ b/docs/blocks/event/test.js @@ -10,7 +10,13 @@ describe('event', function() { it('should check of roles are changed to new value before checklist-change is fired', function() { element(s+'button[ng-click="checkFirst()"]').click(); check(a, [1,0,0,0]); - expect(element(s+'pre').text()).toBe('"a"'); + expect(element(s+'pre').text()).toEqual("\"a\""); + }); + + it('should check that checklist-before-change can deny change of values', function() { + element(s+'button[ng-click="checkAll()"]').click(); + check(a, [1,1,0,1]); + expect(element(s+'pre').text()).toEqual("\"a,c,u\""); }); }); \ No newline at end of file diff --git a/docs/blocks/event/view.html b/docs/blocks/event/view.html index fdc86a1..4ad4bb3 100644 --- a/docs/blocks/event/view.html +++ b/docs/blocks/event/view.html @@ -1,3 +1,3 @@ \ No newline at end of file diff --git a/docs/index.jade b/docs/index.jade index 6573f57..f8b28e8 100644 --- a/docs/index.jade +++ b/docs/index.jade @@ -6,7 +6,6 @@ html(ng-app='app') title Checklist-model - AngularJS directive for list of checkboxes // Bootstrap 3 - script(src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js") link(href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css", rel="stylesheet", media="screen") link(href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css", rel="stylesheet", media="screen") diff --git a/index.html b/index.html index ade9c2f..741d803 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,6 @@ Checklist-model - AngularJS directive for list of checkboxes - @@ -831,7 +830,7 @@

Event

demo