Ox.api: update(items) signature is better (allows for add/remove); Ox.List: update accordingly

This commit is contained in:
rolux 2014-04-30 13:39:14 +02:00
parent 777fbc2a37
commit c26a13b2f1
2 changed files with 9 additions and 8 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_) {
@ -1837,13 +1837,15 @@ 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
index = Ox.getIndexById(self.items, id); self.items[Ox.getIndexById(self.items, id)][key] = value;
self.items[index][key] = value; updateItems = true;
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.update(self.items);
}
return that; return that;
} }
}; };

View file

@ -306,11 +306,10 @@ Ox.api = function(items, options) {
ret = Ox.extend( ret = Ox.extend(
api.cache ? Ox.cache(fn, {async: true}) : fn, api.cache ? Ox.cache(fn, {async: true}) : fn,
{ {
update: function(index, item) { update: function(updatedItems) {
items[index] = item; items = updatedItems;
api.cache && ret.clear(); api.cache && ret.clear();
sortBy.clear(); sortBy.clear();
return items;
} }
} }
), ),