add Ox.getIndex and Ox.getObject; Ox.List: use Ox.getIndex when updating item value

This commit is contained in:
rolux 2014-04-30 14:19:09 +02:00
parent 38b7072822
commit 62c65026b1
2 changed files with 18 additions and 6 deletions

View file

@ -1834,7 +1834,9 @@ Ox.List = function(options, self) {
self.options.items[pos][key] = value; self.options.items[pos][key] = value;
} else if (self.items) { } else if (self.items) {
// items array was passed to initialize the list // items array was passed to initialize the list
self.items[Ox.getIndexById(self.items, id)][key] = value; self.items[
Ox.getIndex(self.items, self.options.unique, id)
][key] = value;
updateItems = true; updateItems = true;
} }
data[key] = value; data[key] = value;

View file

@ -494,6 +494,13 @@ Ox.flatten = function(array) {
return ret; return ret;
}; };
// FIXME: add docs and tests
Ox.getIndex = function(array, key, value) {
return Ox.indexOf(array, function(obj) {
return obj[key] === value;
});
};
/*@ /*@
Ox.getIndexById <f> Returns the first array index of an object with a given id Ox.getIndexById <f> Returns the first array index of an object with a given id
(array, id) -> <n> Index (or `-1`) (array, id) -> <n> Index (or `-1`)
@ -505,9 +512,13 @@ Ox.getIndexById <f> Returns the first array index of an object with a given id
-1 -1
@*/ @*/
Ox.getIndexById = function(array, id) { Ox.getIndexById = function(array, id) {
return Ox.indexOf(array, function(obj) { return Ox.getIndex(array, 'id', id);
return obj.id === id; };
});
// FIXME: add docs and tests
Ox.getObject = function(array, key, value) {
var index = Ox.getIndex(array, key, value);
return index > -1 ? array[index] : null;
}; };
/*@ /*@
@ -521,8 +532,7 @@ Ox.getObjectById <f> Returns the first object in an array with a given id
null null
@*/ @*/
Ox.getObjectById = function(array, id) { Ox.getObjectById = function(array, id) {
var index = Ox.getIndexById(array, id); return Ox.getObject(array, 'id', id);
return index > -1 ? array[index] : null;
}; };
/* /*