Class: Collection

Collection(data)

new Collection(data)

Create a new Collection object

Parameters:
Name Type Description
data Object

An object that can later be used to set instance variables

Example
let collection = new Collection({id: "foo"})
collection.setVarsFromData();

Extends

Members

collectionName :String

Convenience function to get the constructor name of the class that is extending this Collection.

Type:
  • String
Example
class UserCollection extends Collection {...}
let userCollection = new UserCollection();
console.log(userCollection.collectionName) // UserCollection

data :Object

Get and Set the data property. Can be set by passing into constructor or any time after.

Type:
  • Object
Example
collection.data = {id: "foo", count: 5};

model :Object

Get and Set the model property. If set, any plain objects pushed to models will be converted to this type.

Type:
  • Object
Example
class UserModel {...}
let userCollection = new UserCollection();
userCollection.model = UserModel;

model :Array.<Object>

List of models that the collection contains.

Type:
  • Array.<Object>
Example
class UserModel {...}
let userModel1 = new UserModel();
let userModel2 = new UserModel();
let userCollection = new UserCollection();
userCollection.model = UserModel;
userCollection.models = [userModel1, userModel2];

modelIds :Array.<Object>

Returns a list of ids from 'models'. class UserModel {...} let userModel1 = new UserModel({id: 1}); let userModel2 = new UserModel({id: 2}); let userCollection = new UserCollection(); userCollection.model = UserModel; userCollection.models = [userModel1, userModel2]; userCollection.modelIds // [1,2]

Type:
  • Array.<Object>

uniqueModelIds :Array.<Object>

Returns a list of unique ids from 'models'. class UserModel {...} let userModel1 = new UserModel({id: 1}); let userModel2 = new UserModel({id: 2}); let userModel3 = new UserModel({id: 2}); let userCollection = new UserCollection(); userCollection.model = UserModel; userCollection.models = [userModel1, userModel2, userModel3]; userCollection.modelIds // [1,2]

Type:
  • Array.<Object>

Methods

chunk(list, size) → {Array.<Array>}

Splits an array into chunks of a specified size.

Parameters:
Name Type Description
list Array

The array to be split into chunks.

size number

The size of each chunk.

Inherited From:
Returns:

An array containing the chunks of the original array.

Type
Array.<Array>
Examples
// returns [[1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4, 5], 2);
// returns [[1, 2, 3], [4, 5]]
chunk([1, 2, 3, 4, 5], 3);

delay(milliseconds)

Add an async execution delay

Parameters:
Name Type Description
milliseconds Number
Inherited From:
Example
let userModel = new UserModel({id: "foo"})
await userModel.delay(5000); // delay execution by 5000ms
userModel.doSomething();

getObjectFromKeys(keys, orderByAlphaopt) → {object}

Returns an object with keys and values picked from class members. Will not return a key/value pair if value is undefined.

Parameters:
Name Type Attributes Default Description
keys Array.<String>

List of keys

orderByAlpha Boolean <optional>
true

Returned keys will be in alphabetical order unless this param is set to false

Inherited From:
Returns:

Object with keys and values picked from class members.

Type
object
Example
let userModel = new UserModel({id: "foo", count: 4, email: "private@example.com"})
console.log(userModel.getObjectFromKeys(["id", "count"]) // {count: 4, id: "foo"}

matchModelsAndObjects(objects, objectProp, modelProp, callbackFn)

Given a list of objects and properties to find, will return the first model that matches thoe properties.

Parameters:
Name Type Description
objects Array.<Object>

A list of objects.

objectProp String

The property name of the given objects to match against

modelProp String

The property name to search for in models

callbackFn Collection~matchModelsAndObjectsCallback

The callback function that will be called when an object and model are matched.

Example
let collection = new Collection({
  models: [{id: 1, actorId: 1}, {id:2, actorId: 2}]
});
collection.setVarsFromData();
let objects = [{id: 1, name: "Dan"}, {id: 2, name: "Jon"}];
collection.matchModelsAndObjects(objects, "id", "actorId", (model, object) => {
 model.name = object.name;
});

pushModels(list)

Push models or objects to the 'models' property. If given models that match 'model' they will be added. If given plain objects they will be converted to models (assuming 'model' is set). If 'model' is not set, they will be added as plain objects.

Parameters:
Name Type Description
list Array.<Object>

A list of models or plain objects.

Example
let collection = new Collection({model: UserModel});
collection.pushModels([model1, model2]);
collection.pushModels([obj1, obj2]);

setVarsFromData()

Will set class members from all keys and values of the data member. If you pass in 'model' as a key and the Model as a value, that will be used to create models when a 'models' array is also passed in. The 'models' array can be plain objects that will be turned into models, or can be fully formed models.

Overrides:
Example
let collection = new Collection({model: Model, models: [model, model, ...]});
collection.setVarsFromData(); // collection.model now set to Model models
passed in will now be set on collection.models.

setVarsFromObject(object)

Will set class members from all keys and values of the object provided.

Parameters:
Name Type Description
object Object
Inherited From:
Example
let userModel = new UserModel({id: "foo"})
userModel.setVarsFromObject({count: 1}); // userModel.count has now been set

toJSON() → {Array.<Object>}

Method that will allow JSON.stringify(collection) to return a list of models.

Returns:
Type
Array.<Object>

validateBoolean(boolean, options)

Validate a boolean

Parameters:
Name Type Description
boolean Boolean

The boolean to be validated

options Object
Properties
Name Type Attributes Default Description
required Boolean <optional>
true
Inherited From:
Throws:
Example
set hasUsername(boolean) {
  this.validateBoolean(boolean, {name: "hasUsername"})
  this.#hasUsername = boolean;
}

validateEmail(emailAddress, options)

Validate an email address

Parameters:
Name Type Description
emailAddress String

The email address to be validated

options Object
Properties
Name Type Attributes Default Description
required Boolean <optional>
true
Inherited From:
Throws:
Example
set email(emailAddress) {
  this.validateEmail(emailAddress, {name: "email"})
  this.#email = emailAddress;
}

validateInstanceOf(object, constructor, options)

Validate an object is an instance of another object

Parameters:
Name Type Description
object Object

The object to be validated

constructor Object

The constructor to be validated against

options Object
Properties
Name Type Attributes Default Description
required Boolean <optional>
true
Inherited From:
Throws:
Example
set userModel(userModel) {
  this.validateInstanceOf(userModel, UserModel, {name: "userModel"})
  this.#userModel = userModel;
}

validateNumber(number, options)

Validate a number

Parameters:
Name Type Description
number Number

The number to be validated

options Object
Properties
Name Type Attributes Default Description
required Boolean <optional>
true

If true, will check the string for undefined, null, and empty

minValue Number <optional>
Number.MIN_SAFE_INTEGER

If set will check the number for a minimum value.

maxValue Number <optional>
Number.MAX_SAFE_INTEGER

If set will check the number for a maximum value.

name String <optional>

Set to have better error messaging, ie error.message = "UserModel id is required."

Inherited From:
Throws:
Example
set count(number) {
  this.validateNumber(number, {name: "count"})
  this.#count = number;
}

validateRequiredProperties(keys, checkForNullopt)

Validate that certain properties are set on the model. By default will check for undefined and null.

Parameters:
Name Type Attributes Default Description
keys Array.<String>

List of keys

checkForNull Boolean <optional>
true

If set to false will only check for undefined (not null)

Inherited From:
Throws:
Example
let userModel = new UserModel({id: "foo"})
userModel.setVarsFromData();
userModel.validateRequiredProperties(["id", "count"]); // will throw an
error because 'count' is undefined.

validateString(string, options)

Validate a string

Parameters:
Name Type Description
string String

The string to be validated

options Object
Properties
Name Type Attributes Default Description
required Boolean <optional>
true

If true, will check the string for undefined, null, and empty

minLength Number <optional>

If set will check the string for a minimum length of characters.

maxLength Number <optional>
100000

If set will check the string for a maximum length of characters.

name String <optional>

Set to have better error messaging, ie error.message = "UserModel id is required."

Inherited From:
Throws:
Example
set id(string) {
  this.validateString(string, {name: "id"})
  this.#id = string;
}

validateUrl(url, options)

Validate a url

Parameters:
Name Type Description
url String

The url to be validated

options Object
Properties
Name Type Attributes Default Description
required Boolean <optional>
true
protocols Array.<String> <optional>
[http:, https:]

By default a url must have an http: or https: protocol. Pass in a list of protocols to override this.

Inherited From:
Throws:
Examples
set imageUrl(url) {
  this.validateUrl(url, {name: "imageUrl"})
  this.#imageUrl = url;
}
set ftpUrl(url) {
  this.validateUrl(url, {protocols: ["ftp:"]})
  this.#ftpUrl = url;
}

Type Definitions

matchModelsAndObjectsCallback(model, foundObject)

This callback is displayed as a global member.

Parameters:
Name Type Description
model Object

model that was matched.

foundObject Object

object that was matched.