Ox.api: add update method (index, item); Ox.List: when initialized with array of items, update api when changing an item value

This commit is contained in:
rolux 2014-04-30 13:26:56 +02:00
parent ef5a51d6bc
commit 777fbc2a37
2 changed files with 19 additions and 15 deletions

View file

@ -1819,14 +1819,14 @@ Ox.List = function(options, self) {
id = args.shift(), id = args.shift(),
pos = Ox.isNumber(id) ? id : getPositionById(id), pos = Ox.isNumber(id) ? id : getPositionById(id),
$item = self.$items[pos], $item = self.$items[pos],
data = $item ? $item.options('data') : {}, data = $item ? $item.options('data') : {};
updateItems = false;
if (arguments.length == 1) { if (arguments.length == 1) {
return data; return data;
} else if (arguments.length == 2 && Ox.isString(arguments[1])) { } else if (arguments.length == 2 && Ox.isString(arguments[1])) {
return data[arguments[1]]; return data[arguments[1]];
} else if ($item) { } else if ($item) {
Ox.forEach(Ox.makeObject(args), function(value, key) { Ox.forEach(Ox.makeObject(args), function(value, key) {
var index;
if (key == self.options.unique) { if (key == self.options.unique) {
// unique id has changed // unique id has changed
self.options.selected = self.options.selected.map(function(id_) { self.options.selected = self.options.selected.map(function(id_) {
@ -1835,22 +1835,15 @@ Ox.List = function(options, self) {
} }
if (!self.isAsync) { if (!self.isAsync) {
self.options.items[pos][key] = value; self.options.items[pos][key] = value;
} else if(self.items) { } else if (self.items) {
self.items[Ox.getIndexById(self.items, id)][key] = value; // items array was passed to initialize the list
updateItems = true; index = Ox.getIndexById(self.items, id);
self.items[index][key] = value;
self.options.items.update(index, self.items[index]);
} }
data[key] = value; data[key] = value;
}); });
$item.options({data: data}); $item.options({data: data});
if (updateItems) {
self.options.items = Ox.api(self.items, {
cache: true,
map: self.options.map,
sort: self.options.sort,
sums: self.options.sums,
unique: self.options.unique
});
}
return that; return that;
} }
}; };

View file

@ -303,6 +303,17 @@ Ox.api = function(items, options) {
callback && callback(result); callback && callback(result);
return result; return result;
}, },
ret = Ox.extend(
api.cache ? Ox.cache(fn, {async: true}) : fn,
{
update: function(index, item) {
items[index] = item;
api.cache && ret.clear();
sortBy.clear();
return items;
}
}
),
sortBy = Ox.cache(function sortBy(array, by, map, query) { sortBy = Ox.cache(function sortBy(array, by, map, query) {
return Ox.sortBy(array, by, map); return Ox.sortBy(array, by, map);
}, { }, {
@ -423,7 +434,7 @@ Ox.api = function(items, options) {
return match; return match;
} }
return api.cache ? Ox.cache(fn, {async: true}) : fn; return ret;
}; };