You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/collections/collection.js

81 lines
1.4 KiB

app.Collection = class Collection {
constructor(objects) {
1 year ago
if (objects == null) {
objects = [];
}
this.reset(objects);
}
model() {
return app.models[this.constructor.model];
}
reset(objects) {
1 year ago
if (objects == null) {
objects = [];
}
this.models = [];
for (var object of objects) {
1 year ago
this.add(object);
}
}
add(object) {
if (object instanceof app.Model) {
this.models.push(object);
} else if (object instanceof Array) {
for (var obj of object) {
1 year ago
this.add(obj);
}
} else if (object instanceof app.Collection) {
this.models.push(...(object.all() || []));
} else {
this.models.push(new (this.model())(object));
}
}
remove(model) {
this.models.splice(this.models.indexOf(model), 1);
}
size() {
return this.models.length;
}
isEmpty() {
return this.models.length === 0;
}
each(fn) {
for (var model of this.models) {
1 year ago
fn(model);
}
}
all() {
return this.models;
}
contains(model) {
return this.models.includes(model);
}
findBy(attr, value) {
return this.models.find((model) => model[attr] === value);
}
findAllBy(attr, value) {
return this.models.filter((model) => model[attr] === value);
}
countAllBy(attr, value) {
let i = 0;
for (var model of this.models) {
1 year ago
if (model[attr] === value) {
i += 1;
}
}
return i;
}
};