BikeModel
Extends:
Class for the bike model to be used by the BikePresenter and AddBikePresenter
Constructor Summary
Public Constructor | ||
public |
Creates an instance of BikeModel. |
Member Summary
Public Members | ||
public |
listener: * |
Private Members | ||
private |
_callback: * |
|
private |
_data: {"data": *} |
|
private |
_getBikeByID(id: string): Object: * Returns the bike data by providing the bike ID. |
Method Summary
Public Methods | ||
public |
deleteBikeByID(id: string, callback: Function) Delete a bike from the database |
|
public |
Get method for presenters to get data. |
|
public |
setCallback(callback: Function) Set the model's callback to a new callback. This callback can be used anywhere and is usually passed in from a presenter. |
|
public |
Toggle the database listener off and then on again to get the data again. |
|
public |
Update method for presenters to update the model's data. Datetime and Owner are handled in database class. Callback needs to be set with BikeM.setCallback(callback); callback takes in 1 parameter. |
Private Methods | ||
private |
_bikeDataExists(bikeData: Object): Boolean, Number Checks if the bike exists based on the data of the bike. |
|
private |
_bikeIDExists(id: string): Boolean, Number Checks if the bike exists based on the id. |
|
private |
async _checkForLocalData(key: string) Checks if local data is stored, and if so, update the data. |
|
private |
_checkImages(index: Number, thumbnails: List): Boolean Checks if there are new images in the bike stored vs what was passed in. |
|
private |
_defaultCallback(message: *) Default callback |
|
private |
_editExistingInDatabase(newData: Object, callback: Function): * Overwrite existing data in database and call the function callback depending on if it was successful or not. |
|
private |
_hasProperty(obj: Object, property: string): Boolean Checks if an object has a certain property. |
|
private |
_insertDataOnRead(databaseData: Object) Insert data into the data object on a read from the database. |
|
private |
_insertDataOnUpdate(newData: Object, exists: Boolean, index: Number): Boolean Insert data into the data object on an update trigger (from Presenter). |
|
private |
Register an 'on' read from the database to get updates anytime data changes in the database. |
|
private |
_removeIllustrationKey(thumbnails: List): List Removes the illustration key from the object and only adds the actual link. |
|
private |
async _saveDataToLocalStorage(key: string, data: Object) Save data to local storage. |
|
private |
_writeImageToDBStorage(id: Number, images: List, imagesFolder: string, onSuccess: Function, onError: Function) Write the image to the firebase storage and call the callbacks with the urls that were defined. |
|
private |
_writeNewInDatabase(newData: Object, callback: Function): * Write new data in database and call the function callback depending on if it was successful or not. |
Public Constructors
public constructor() source
Creates an instance of BikeModel. Sets the default callback, creates an observerlist, and registers an on read from the database.
Public Members
public listener: * source
Private Members
private _callback: * source
private _data: {"data": *} source
Public Methods
public setCallback(callback: Function) source
Set the model's callback to a new callback. This callback can be used anywhere and is usually passed in from a presenter.
Params:
Name | Type | Attribute | Description |
callback | Function | A callback to run when certain code is executed |
public toggleListeners() source
Toggle the database listener off and then on again to get the data again. TODO : Better method to do this?
public update(newData: Object) source
Update method for presenters to update the model's data. Datetime and Owner are handled in database class. Callback needs to be set with BikeM.setCallback(callback); callback takes in 1 parameter.
Params:
Name | Type | Attribute | Description |
newData | Object | New data to add |
Private Methods
private _bikeDataExists(bikeData: Object): Boolean, Number source
Checks if the bike exists based on the data of the bike.
Params:
Name | Type | Attribute | Description |
bikeData | Object | The data to check |
Return:
Boolean, Number | exists: true: If the bike exists; false: otherwise. index - The index of the bike if it exists, -1 if not |
private _bikeIDExists(id: string): Boolean, Number source
Checks if the bike exists based on the id.
Params:
Name | Type | Attribute | Description |
id | string | The id of a bike |
Return:
Boolean, Number | exists: true: If the bike exists; false: otherwise. index - The index of the bike if it exists, -1 if not |
private async _checkForLocalData(key: string) source
Checks if local data is stored, and if so, update the data. This is because we don't wait for this data when authenticating and the user can see their bikes if offline.
Params:
Name | Type | Attribute | Description |
key | string | Key to get data for |
private _checkImages(index: Number, thumbnails: List): Boolean source
Checks if there are new images in the bike stored vs what was passed in.
Params:
Name | Type | Attribute | Description |
index | Number | The index of the bike in the local data |
|
thumbnails | List | A list of thumbnails |
Return:
Boolean | true: If the thumbnails are the same; false: If the thumbnails are different or if the bike doesn't exist |
private _defaultCallback(message: *) source
Default callback
Params:
Name | Type | Attribute | Description |
message | * |
private _editExistingInDatabase(newData: Object, callback: Function): * source
Overwrite existing data in database and call the function callback depending on if it was successful or not.
Return:
* |
private _hasProperty(obj: Object, property: string): Boolean source
Checks if an object has a certain property.
private _insertDataOnRead(databaseData: Object) source
Insert data into the data object on a read from the database.
Params:
Name | Type | Attribute | Description |
databaseData | Object | An objects of objects containing data from the database. |
private _insertDataOnUpdate(newData: Object, exists: Boolean, index: Number): Boolean source
Insert data into the data object on an update trigger (from Presenter).
private _registerDBReadListener() source
Register an 'on' read from the database to get updates anytime data changes in the database.
private _removeIllustrationKey(thumbnails: List): List source
Removes the illustration key from the object and only adds the actual link.
Params:
Name | Type | Attribute | Description |
thumbnails | List | A list of thumbnail objects with the property 'illustration' |
Return:
List | A list of thumbnails |
private _writeImageToDBStorage(id: Number, images: List, imagesFolder: string, onSuccess: Function, onError: Function) source
Write the image to the firebase storage and call the callbacks with the urls that were defined.
Params:
Name | Type | Attribute | Description |
id | Number | The id of the bike corresponding to the image |
|
images | List | A list of objects with the property 'illustration' |
|
imagesFolder | string | The folder to upload images to |
|
onSuccess | Function | A callback to call when an image has been successfully uploaded |
|
onError | Function | A callback to call when an image has failed to upload |