Created
February 10, 2014 07:47
-
-
Save elado/8911924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app = angular.module 'checkboxListApp' | |
app.directive 'checkboxList', -> | |
restrict: 'AE' | |
replace: true | |
scope: | |
list: '=' | |
model: '=ngModel' | |
link: (scope, element, attrs) -> | |
updateMap = -> | |
ids = _.pluck(scope.model, 'id').map(String) | |
scope.map = _.reduce(ids, ((all, id) -> all[id] = true; all), {}) | |
scope.$watch 'model', updateMap | |
updateMap() | |
scope.update = -> | |
scope.model = _.reduce scope.map, (all, state, id) -> | |
if state | |
id = String(id) | |
found = _.find scope.list, (item) -> String(item.id) == id | |
all.push found if found | |
all | |
, [] | |
template: """ | |
<div class="checkbox-group"> | |
<div class="checkbox" ng-repeat="item in list"> | |
<label> | |
<input type="checkbox" ng-model="map[item.id]" ng-change="update()"> | |
{{item.name}} | |
</label> | |
</div> | |
</div> | |
""" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<checkbox-list | |
list="categories" | |
ng-model="product.categories" | |
></checkbox-list> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment