Class: BaseObject

BaseObject

The base class for all project objects. Provides data mapping functionality.

new BaseObject (options)

Name Type Description
options Object

Properties to be assigned to the BaseObject. Must match the keys of the BaseObject's responseMap or valid options defined by the class's constructor.

Members

displayName String

The class name. Minification will break this.constructor.name; this allows for readable logging even in minified code.

Methods

BaseObject.buildFromServer (data, constructorParams)BaseObject static

Returns a new instance of the BaseObject populated with the passed data that came from ESPN, mapping the attributes defined in the value of responseMap to the matching key. Use this method when constructing BaseObjects with server responses.

Name Type Description
data Object

Data originating from the server.

constructorParams Object

Params to be passed to the instance's constructor. Useful for passing parent data, such as leagueId.

Returns:
Type Description
BaseObject A new instance of the BaseObject populated with the passed data.

Type Definitions

ResponseMapValueObject Object

The responseMap can have two values: a string or a ResponseMapValueObject. When string, the data found on that response is directly mapped to the BaseObject without mutation. When ResponseMapValueObject, the data at the key will be used to create BaseObject(s) or manually parsed with a provided manualParse function. Either result is attached to the BaseObject being populated.

Properties:
Name Type Description
key String

The key on the response data where the data can be found. This must be defined.

BaseObject BaseObject

The BaseObject to create with the response data.

isArray Boolean

Whether or not the response data is an array. Useful for attributes such as "teams".

defer Boolean

Whether or not to wait to parse the entry until a second pass of the map. This is useful for populating items with cached instances that are not guaranteed to be parsed/cached during initial parsing. Example: Using Team instances on League.

manualParse function

A function to manually apply logic to the response. This function must return its result to be attached to the populated BaseObject. The arguments to this function are: (data at the key), (the whole response), (the instance being populated).

Example
static responseMap = {
  teamId: 'teamId',
  team: {
    key: 'team_on_response',
    BaseObject: true
  },
  teams: {
    key: 'teams_on_response',
    BaseObject: Team,
    isArray: true
  },
  manualTeams: {
    key: 'manual_teams_on_response',
    BaseObject: Team,
    manualParse: (responseData, response, constructorParams, instance) => (
      Team.buildFromServer(responseData)
    )
  }
};