Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
fix(options): do not identify options by index, otherwise changing op…
Browse files Browse the repository at this point in the history
…tions won't work

use hash code instead of index for 'track by'
  • Loading branch information
Philipp Burgmer committed Apr 9, 2014
1 parent 1a9f6b5 commit 0cb34de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
"laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
"laxcomma" : true, // Tolerate line breaking before ','
"bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.).
"bitwise" : false, // Prohibit bitwise operators (&, |, ^, etc.).
"boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
"curly" : true, // Require {} for every new block or scope.
"eqeqeq" : true, // Require triple equals i.e. `===`.
Expand Down
55 changes: 53 additions & 2 deletions src/w11k-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ angular.module('w11k.select').directive('w11kSelect', [
var optionsExpParsed = optionParser.parse(optionsExp);

function collection2options(collection, viewValue) {
return collection.map(function (option, index) {
return collection.map(function (option) {
var optionValue = modelElement2value(option);
var optionLabel = modelElement2label(option);

Expand All @@ -361,7 +361,7 @@ angular.module('w11k.select').directive('w11kSelect', [
}

return {
index: index,
hash: hashCode(option).toString(36),
label: optionLabel,
model: option,
selected: selected
Expand Down Expand Up @@ -552,6 +552,57 @@ angular.module('w11k.select').directive('w11kSelect', [

return optionsExpParsed.label(context);
}

// inspired by https://github.com/stuartbannerman/hashcode
var hashCode = (function () {
var stringHash = function (string) {
var result = 0;
for (var i = 0; i < string.length; i++) {
result = (((result << 5) - result) + string.charCodeAt(i)) | 0;
}
return result;
};

var primitiveHash = function (primitive) {
var string = primitive.toString();
return stringHash(string);
};

var objectHash = function (obj) {
var result = 0;
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
result += primitiveHash(property + hash(obj[property]));
}
}
return result;
};

var hash = function (value) {
var typeHashes = {
'string' : stringHash,
'number' : primitiveHash,
'boolean' : primitiveHash,
'object' : objectHash
// functions are excluded because they are not representative of the state of an object
// types 'undefined' or 'null' will have a hash of 0
};

var type = typeof value;

if (value === null || value === undefined) {
return 0;
}
else if (typeHashes[type] !== undefined) {
return typeHashes[type](value) + primitiveHash(type);
}
else {
return 0;
}
};

return hash;
})();
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/w11k-select.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</div>
<div class="content" infinite-scroll="showMoreOptions()" infinite-scroll-distance="0.5">
<ul class="items list-unstyled">
<li bindonce="option" ng-repeat="option in optionsToShow track by option.index" ng-click="select(option)" ng-class="{'selected': option.selected }">
<li bindonce="option" ng-repeat="option in optionsToShow track by option.hash" ng-click="select(option)" ng-class="{'selected': option.selected }">
<div class="state">
<input type="checkbox" ng-model="option.selected" ng-change="onOptionStateChange()" ng-click="onOptionStateClick($event)">
</div>
Expand Down

0 comments on commit 0cb34de

Please sign in to comment.