Reference Source
public class | source

BikeModel

Extends:

* → BikeModel

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
Private Members
private
private

_data: {"data": *}

private

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(): Object

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(newData: Object)

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

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

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

private _getBikeByID(id: string): Object: * source

Returns the bike data by providing the bike ID.

Return:

Object

The data coresponding to the bike id

Public Methods

public deleteBikeByID(id: string, callback: Function) source

Delete a bike from the database

Params:

NameTypeAttributeDescription
id string

A bike id to delete

callback Function

A function to call when remove succeeds or fails

public get(): Object source

Get method for presenters to get data.

Return:

Object

data stored in the model

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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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.

Params:

NameTypeAttributeDescription
newData Object

Data to be written to the database

callback Function

A function to call on the success or failure of the call

Return:

*

private _hasProperty(obj: Object, property: string): Boolean source

Checks if an object has a certain property.

Params:

NameTypeAttributeDescription
obj Object

An object to check

property string

The name of a property

Return:

Boolean

true: if the object has the property; false: otherwise

private _insertDataOnRead(databaseData: Object) source

Insert data into the data object on a read from the database.

Params:

NameTypeAttributeDescription
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).

Params:

NameTypeAttributeDescription
newData Object

New data passed in, of the form : {data: []}

exists Boolean

If the bike already exists

index Number

The index of the bike. Positive if it exists, negative if it doesn't

Return:

Boolean

true: Data was an edited value; false: Data was a new value

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:

NameTypeAttributeDescription
thumbnails List

A list of thumbnail objects with the property 'illustration'

Return:

List

A list of thumbnails

private async _saveDataToLocalStorage(key: string, data: Object) source

Save data to local storage.

Params:

NameTypeAttributeDescription
key string

A key to store the data under

data Object

The data to store

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:

NameTypeAttributeDescription
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

private _writeNewInDatabase(newData: Object, callback: Function): * source

Write new data in database and call the function callback depending on if it was successful or not.

Params:

NameTypeAttributeDescription
newData Object

Data to be written to the database

callback Function

A function to call on the success or failure of the call

Return:

*