I'm using the jQuery Isotope plugin (http://isotope.metafizzy.co/) which is awesome, but I have a problem with creating the sorts. It's more of a JavaScript problem than anything to do with Isotope itself.
The problem is that I'm building the sort data dynamically. So I've created a function that makes the sortData. An example below:
function getSortData(){
sortData = {};
var sorts = ['symbol', 'number', 'score', 'name'];
for (var i in sorts) {
sortData[sorts[i]] = function($elem) {
console.log(sorts[i]);
return parseInt($elem.find('.'+ sorts[i]).text());
}
}
return sortData;
}
So the problem is that the anonymous function inside always runs after the entire getSortData() function has run. Resulting in the last item in the sorts array being assigned to the sort[i] variable.
This is shown in this fiddle: http://jsfiddle.net/xzZR4/ You'll see that the 'name' item is always outputted to the console.
I can't think of another way to create the getSortData object that will allow the correct sort name to be passed.
Anyone with any ideas?