DataBehavior

DataBehavior

new DataBehavior()

Source:
Author:
  • jyoung@vecna.com
See:

This behavior implements simplified interaction with data sources (i.e. TorsoCollection).
This behavior manages re-rendering when data changes and automatically adding the returned data to the view's context.
This behavior also manages dependencies between data and other objects to allow intelligent re-fetching when data changes.

Extends

Classes

Data

Members

(static) alwaysFetch

Source:
Default Value:
  • false
Properties:
Name Type Description
alwaysFetch boolean

Determines whether pull() or fetch() is called when using retrieve().
true - Use fetch() by default on the private collection.
false (default) - Use pull() by default on the private collection.
True will query the server more often, but will provide more up-to-date data.
False will only query the server if the model hasn't already been retrieved.
This property will be ignored if fetch() or pull() is called directly.

(static) cache

Source:
Properties:
Name Type Description
cache Collection

The torso collection that is acting as a cache used to create the private collections.
This property/option is required. Instantiation will fail if it is not set.

(static) data

Source:
Properties:
Name Type Description
data Torso.behaviors.DataBehavior.Data

Object that manages interaction with the data. Contains the privateCollection, proxies all events from the privateCollection,
and has get('...') and .toJSON() methods that access the private collection data.

(static) FETCHED_STATUSES

Source:
Properties:
Name Type Description
{ Object

SUCCESS: 'SUCCESS', FAILURE: 'FAILURE' } FETCHED_STATUSES

The possible fetched statuses. This is the status value of the fetched event payload.

(static) ids

Source:
Properties:
Name Type Description
ids string | number | Array.<string> | Array.<number> | Object | function

Duck-typed property that identifies the ids to use. id or ids is required (either by behavior options or as properties).

  • {(string|number)} - the id to use directly (equivalent to an array of a single id).

  • {(string[]|number[])} - the ids to use directly.

  • {Object} - more complex configuration that identifies a model-like object that fires a change event and the
    property on that object to use. The object can fire the change event for the given property
    and have a .get('propertyName') method, or it can define the property directly on the idContainer.
    Only one property can be identified as supplying the id for this data model.
    If the identified object does not fire a change event then the id(s) will never be refreshed for this behavior.
    The idContainer can also fire a 'fetched:ids' event on itself to signal to this data behavior that the ids
    have been fetched for the first time. Then a 'change:' event can be used to notify this
    data behavior that the property has been modified.

    • property {string} - the name of the property that defines the ids. The root object is assumed to be the view unless
      idContainer is defined. The idContainer is the object that fires a change event for the given property name.
      Uses the view or the idContainer as the root to get the identified property (i.e. 'viewState.', 'model.', etc).
      Will get the property before the first '.' from the view and if it is an object will try to use a
      .get('') on it and set a 'change:' listener on it.
      If it is a string/number or array of string/number, then it will use that as the ids.
      Triggering a 'id-container-updated' event on the behavior will cause it to stop listing to the
      old idContainer and start listening to the new one defined by this property.
    • idContainer {Cell|Backbone.Model|Function} - object (or a function that returns an object) that fires change
      events and has a .get('propertyName') function. It isn't required to fire events -
      the change event is only required if it needs to re-fetch when the id property value changes.
      Examples:
      • { property: '_patientId' }
      • { property: 'viewState.appointmentId' }
      • { property: 'model.type' }
      • { property: 'behaviors.demographics.data.appointments' }
      • { property: 'id', idContainer: userService }
      • { property: 'username', idContainer: function() { application.getCurrentUser() } }
  • {Function(cache)} - expected to return the ids (either array, jquery deferred that resolves to the ids or single primitive)
    to track with the private collection. Cache is passed in as the first argument so that the behavior
    can be defined and the cache can be overridden later.
    'this' is the behavior (from which you can get the view if needed).
    What was criteria should use this instead:

    function(cache) {
      var thisBehaviorInstance = this;
      var view = this.view;
      var criteria = { ... some criteria ... };
      return cache.fetchIdsByCriteria(criteria);
    }

(static) renderOnFetch

Source:
Default Value:
  • false
Properties:
Name Type Description
renderOnFetch boolean

Adds a listener on the Behavior for the fetched event that triggers a render on the view.
true - A listener is added to the behavior that re-renders the view when a 'fetched' event is triggered.
false (default) - no listeners are added.

(static) returnSingleResult

Source:
Default Value:
  • false
Properties:
Name Type Description
returnSingleResult boolean

Determines the result of view.getBehavior('thisBehaviorAlias').toJSON().
true - a single model result is returned.
false (default) - an array of model results are returned.

(static) skipInitialLoad

Source:
Default Value:
  • false
Properties:
Name Type Description
skipInitialLoad boolean

Skip triggering a load of this data behavior when the view completes initializing.
true - no load after the view is initialized.
false (default) - trigger a .retrieve() on this data behavior when the view completes initialization.

(static) updateEvents

Source:
Properties:
Name Type Description
updateEvents string | Array.<string> | Object | Array.<Object>

cause this behavior to re-calculate its ids and refetch them from the server if the given events are triggered
(space separated if string, single item is equivalent to array of single item).

  • 'view:eventName' - arbitrary event triggered on the view (eventName can be a change:propertyName event).
  • 'viewState:eventName' - arbitrary event triggered on the viewState (eventName can be a change:propertyName event).
  • 'model:eventName' - arbitrary even triggered on the view's model (eventName can be a change:propertyName event).
  • 'this:eventName' - arbitrary event triggered by this behavior (eventName can be a change:propertyName event).
  • 'behaviorAlias:eventName' - arbitrary event triggered by another data behavior on this view (eventName can be a change:propertyName event).
  • 'behaviorAlias.data:eventName' - arbitrary event triggered by the data of another DataBehavior on this view (eventName can be a change:propertyName event).
  • { '': < object (or function returning an object) that the event is triggered on > } - arbitrary ('') triggered on the supplied object.

alias :string

Source:
Inherited From:

Unique name of the behavior instance w/in a view. More human readable than the cid.

Type:
  • string

cidPrefix :string

Source:
Inherited From:

cidPrefix of Behaviors

Type:
  • string

mixin :Object

Source:
Inherited From:

Add functions to be added to the view's public API. They will be behavior-scoped.

Type:
  • Object

Methods

(static) constructor(behaviorStateopt, behaviorOptions, viewOptionsopt)

Source:
Parameters:
Name Type Attributes Description
behaviorState Object <optional>

the initial state of the behavior.

behaviorOptions Object
Properties
Name Type Attributes Default Description
cache Collection

see cache property.

renderOnFetch boolean <optional>
false

see renderOnFetch property.

skipInitialLoad boolean <optional>
false

see skipInitialLoad property.

returnSingleResult boolean <optional>
false

see returnSingleResult property.

alwaysFetch boolean <optional>
false

see alwaysFetch property.

id string | number | Array.<string> | Array.<number> | Object | function <optional>
behaviorOptions.ids

see id property.

ids string | number | Array.<string> | Array.<number> | Object | function <optional>
behaviorOptions.id

see ids property.

updateEvents string | Array.<string> | Object | Array.<Object> <optional>

see updateEvents property.

viewOptions Object <optional>

options passed to View's initialize

(static) fetch() → {$.Deferred.Promise}

Source:

Retrieves the ids for this data object and passes them off to the private collection's trackAndFetch() method.

Returns:

a jquery deferred promise that resolves to the retrieved models.

Type
$.Deferred.Promise

(static) isLoading() → {boolean}

Source:

Determine if the behavior is loading objects or ids.

Returns:

true - the behavior is currently loading objects or ids.
false - the behavior is not currently loading objects or ids.

Type
boolean

(static) isLoadingIds() → {boolean}

Source:

Determine if the behavior is loading ids.

Returns:

true - the behavior is currently loading ids.
false - the behavior is not currently loading ids.

Type
boolean

(static) isLoadingObjects() → {boolean}

Source:

Determine if the behavior is loading objects.

Returns:

true - the behavior is currently loading objects.
false - the behavior is not currently loading objects.

Type
boolean

(static) listenToIdsPropertyChangeEvent()

Source:

Listens for the change event on the ids property and, if triggered, re-fetches the data based on the new ids.

(static) prepare()

Source:

Adds the toJSON of the data represented by this behavior to the context.

(static) pull() → {$.Deferred.Promise}

Source:

Retrieves the ids for this data object and passes them off to the private collection's trackAndPull() method.

Returns:

a jquery deferred promise that resolves to the retrieved models.

Type
$.Deferred.Promise

(static) retrieve() → {$.Deferred.Promise}

Source:

Retrieves the ids for this data object and passes them off to the private collection to track and then does a
pull or a fetch based on the alwaysFetch property. (pull is default if always fetch is true then it fetches instead).

Returns:

a jquery deferred promise that resolves to the retrieved models.

Type
$.Deferred.Promise

(static) retrieveOncePromise() → {external:jQuery-Deferred}

Source:

This is a good way to have something be called after at least one retrieve (pull or fetch) has completed.
This is especially useful if you don't care if the fetch has already happen you just want to do something once
the data is loaded.

This can also be done purely by listening for the 'fetched' event, but you might miss the event if it is fired
before you start listening. This gives a structure for handling that case so that your methods are called
if the event is fired and if it is not fired.

This also gives the ability to distinguish between a successful and failed fetch easily using the promises
resolve/reject handlers.

Usage:

someDataBehavior.retrieveOncePromise()
.then(view.doSomethingWithTheData, view.handleFiledFetch);

Returns:

that resolves when the data is successfully fetched and rejects when the fetch fails.

Type
external:jQuery-Deferred

(static) stopListeningToIdsPropertyChangeEvent()

Source:

Removes the listener added by listenToIdsPropertyChangeEvent().

_dispose()

Source:
Inherited From:

Method to be invoked when dispose is called. By default calling dispose will remove the
behavior's on's and listenTo's.
Override this method to destruct any extra

isDisposed() → {boolean}

Source:
Inherited From:
Returns:

true if the view was disposed

Type
boolean

prepare() → {Object}

Source:
Inherited From:

The behavior's prepare result will be combined with the view's prepare with the behavior's alias as the namespace.
effectively: { [behaviorName]: behavior.prepare() } will be combined with the view's prepare result.

Returns:

a prepare context suitable to being added to the view's prepare result.

Type
Object