new Model(data)
Create a new Model object
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | An object that can later be used to set instance variables |
Example
let model = new Model({id: "foo", count: 5})
model.setVarsFromData();
Extends
Members
data :Object
Get and Set the data property. Can be set by passing into constructor or any time after.
Type:
- Object
Example
model.data = {id: "foo", count: 5};
modelName :String
Convenience function to get the constructor name of the class that is extending this Model.
Type:
- String
Example
class UserModel extends Model {...}
let userModel = new UserModel();
console.log(userModel.modelName) // UserModel
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"}
setVarsFromData()
Will set class members from all keys and values of the data member.
- Inherited From:
Example
let model = new Model({id: "foo"});
model.setVarsFromData(); // model.id now set to "foo"
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
validateBoolean(boolean, options)
Validate a boolean
Parameters:
Name | Type | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
boolean |
Boolean | The boolean to be validated |
||||||||||
options |
Object |
Properties
|
- 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
|
- 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
|
- 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
|
- 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
|
- 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
|
- 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;
}