Skip to content

Instantly share code, notes, and snippets.

@rhettl
Last active December 24, 2015 21:09
Show Gist options
  • Save rhettl/6863488 to your computer and use it in GitHub Desktop.
Save rhettl/6863488 to your computer and use it in GitHub Desktop.
Testing that angular-chosen works with objects
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="chosen/chosen.css"/>
<style>
select {
width: 200px;
}
</style>
</head>
<body>
<section ng-app="ngChosenTest" ng-controller="testCtrl">
<select chosen multiple
ng-model="chosenTasks"
ng-options="task as task.name for (id, task) in tasks">
<option value=""></option>
</select>
<button ng-click="performTasks(chosenTasks)">Run Tasks</button>
</section>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js"></script>
<script src="chosen/chosen.jquery.min.js"></script>
<script src="chosen/ng-chosen.js"></script>
<script>
var app = angular.module('ngChosenTest', ['ngChosen'])
.controller('testCtrl', ['$scope', function($scope){
var forEach = angular.forEach;
$scope.tasks = {
a: {name: 'ABC', func: function(){console.log('Function: ', this.name)}},
b: {name: 'DEF', func: function(){console.log('Function: ', this.name)}},
c: {name: 'GHI', func: function(){console.log('Function: ', this.name)}},
d: {name: 'JKL', func: function(){console.log('Function: ', this.name)}},
e: {name: 'MNO', func: function(){console.log('Function: ', this.name)}},
f: {name: 'PQR', func: function(){console.log('Function: ', this.name)}},
}
$scope.performTasks = function(tasks){
forEach(tasks, function(item){
item.func();
});
}
}]);
</script>
</body>
</html>
// Generated by CoffeeScript 1.6.3
(function() {
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
angular.module('ngChosen', []);
angular.module('ngChosen').directive('chosen', [
'$timeout', '$parse', function($timeout, $parse) {
var CHOSEN_OPTION_WHITELIST, NG_OPTIONS_REGEXP, chosen, isEmpty, snakeCase, equals;
NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w\d]*)|(?:\(\s*([\$\w][\$\w\d]*)\s*,\s*([\$\w][\$\w\d]*)\s*\)))\s+in\s+(.*)$/;
CHOSEN_OPTION_WHITELIST = ['noResultsText', 'allowSingleDeselect', 'disableSearchThreshold', 'disableSearch', 'enableSplitWordSearch', 'inheritSelectClasses', 'maxSelectedOptions', 'placeholderTextMultiple', 'placeholderTextSingle', 'searchContains', 'singleBackstrokeDelete', 'displayDisabledOptions', 'displaySelectedOptions'];
snakeCase = function(input) {
return input.replace(/[A-Z]/g, function($1) {
return "_" + ($1.toLowerCase());
});
};
isEmpty = function(value) {
var key, _i, _len;
if (angular.isArray(value)) {
return value.length === 0;
} else if (angular.isObject(value)) {
for (_i = 0, _len = value.length; _i < _len; _i++) {
key = value[_i];
if (value.hasOwnProperty(key)) {
return false;
}
}
}
return true;
};
equals = angular.equals;
return chosen = {
restrict: 'A',
link: function(scope, element, attr) {
var disableWithMessage, match, options, startLoading, stopLoading, valuesExpr;
options = scope.$eval(attr.chosen) || {};
angular.forEach(attr, function(value, key) {
if (__indexOf.call(CHOSEN_OPTION_WHITELIST, key) >= 0) {
return options[snakeCase(key)] = scope.$eval(value);
}
});
startLoading = function() {
return element.addClass('loading').attr('disabled', true).trigger('chosen:updated');
};
stopLoading = function() {
return element.removeClass('loading').attr('disabled', false).trigger('chosen:updated');
};
disableWithMessage = function(message) {
return element.empty().append("<option selected>" + message + "</option>").attr('disabled', true).trigger('chosen:updated');
};
$timeout(function() {
return element.chosen(options);
});
// if (attr.ngChange && attr.ngModel){
// scope.$watch(attr.ngModel, function(newData, oldData){
// if (!equals(newData, oldData))
// console.log(attr.ngChange);
// $parse(attr.ngChange);
// });
// }
if (attr.ngOptions) {
match = attr.ngOptions.match(NG_OPTIONS_REGEXP);
valuesExpr = match[7];
if (angular.isUndefined(scope.$eval(valuesExpr))) {
startLoading();
}
return scope.$watch(valuesExpr, function(newVal, oldVal) {
if (newVal !== oldVal) {
stopLoading();
if (isEmpty(newVal)) {
return disableWithMessage(options.no_results_text || 'No values available');
}
}
});
}
}
};
}
]);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment