Skip to content

Commit

Permalink
Render after options change
Browse files Browse the repository at this point in the history
  • Loading branch information
Rupert Bedford committed May 31, 2016
1 parent e3de8c6 commit bcb808b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/app/forms/multiple-checkbox.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,31 @@

ngModel.$isEmpty = function(value) {
// Treat empty list as empty
return !value || value.length == 0;
return !value || value.length === 0;
};

// Called when the model is updated
ngModel.$render = function() {
var selected = ngModel.$viewValue || [];
var selectedOptions = ngModel.$viewValue || [];

// Wrap the selected options
selectedOptions = wrapSelectOptions(selectedOptions, scope.optionsId, scope.optionsLabel);

// Update the selected options
_.forEach(scope.localOptions, function(option) {
scope.selected[option.id] = _.includes(selected, option.value);
_.forEach(scope.localOptions, function(localOption) {
scope.selected[localOption.id] = _.some(selectedOptions, function(selectedOption) {
return localOption.id === selectedOption.id;
});
});
};

// Watch for changes to the list of options
scope.$watchCollection('options', function(options) {
// Wrap the options
scope.localOptions = wrapSelectOptions(options, scope.optionsId, scope.optionsLabel);

// Update the selections
ngModel.$render();
});

// An option was selected
Expand All @@ -50,7 +58,7 @@

// Update the model
ngModel.$setViewValue(selected);
}
};
}
};
}]);
Expand Down

0 comments on commit bcb808b

Please sign in to comment.