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

View file

@ -303,6 +303,17 @@ Ox.api = function(items, options) {
callback && callback(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) {
return Ox.sortBy(array, by, map);
}, {
@ -423,7 +434,7 @@ Ox.api = function(items, options) {
return match;
}
return api.cache ? Ox.cache(fn, {async: true}) : fn;
return ret;
};