From 0cb34def4f357fe9b967b2f69a96dd2dd883ba64 Mon Sep 17 00:00:00 2001 From: Philipp Burgmer Date: Wed, 9 Apr 2014 13:11:49 +0200 Subject: [PATCH] fix(options): do not identify options by index, otherwise changing options won't work use hash code instead of index for 'track by' --- .jshintrc | 2 +- src/w11k-select.js | 55 ++++++++++++++++++++++++++++++++++++++-- src/w11k-select.tpl.html | 2 +- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/.jshintrc b/.jshintrc index 99fc66d..f5ae818 100644 --- a/.jshintrc +++ b/.jshintrc @@ -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. `===`. diff --git a/src/w11k-select.js b/src/w11k-select.js index ae03807..399bfae 100644 --- a/src/w11k-select.js +++ b/src/w11k-select.js @@ -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); @@ -361,7 +361,7 @@ angular.module('w11k.select').directive('w11kSelect', [ } return { - index: index, + hash: hashCode(option).toString(36), label: optionLabel, model: option, selected: selected @@ -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; + })(); } }; } diff --git a/src/w11k-select.tpl.html b/src/w11k-select.tpl.html index 4af3e2d..daed288 100644 --- a/src/w11k-select.tpl.html +++ b/src/w11k-select.tpl.html @@ -42,7 +42,7 @@