Class bbq.domain.Entity

Extends bbq.lang.Watchable.
Defined in: Entity.js.

Base class for domain objects. Supports being partially loaded - that is, properties can be asked for from this object in advance of them being loaded. If an unloaded property is requested, a full object load will be trigged.

Extending classes should implement:

  1. The _retrieveURL property. This should be a URL that will answer to a JSON request of the form {id: value} where value is returned from getId() being called on this object
  2. The _getDefaultObject method. This should return an object with keys for every property this object expects to have filled after a call to _retrieveURL.

include(bbq.domain.Entity);

myapp.domain.Room = new Class.create(bbq.domain.Entity, {
    _retrieveURL: "/rooms/get",

    _getDefaultObject: function() {
        return {
            id: null,
            name: null,
            bookings: null
        }
    }
});

Class Summary
Constructor Attributes Constructor Name and Description
 

Base class for domain objects.

Method Summary
Method Attributes Method Name and Description
 
Returns true if this Entity is fully loaded.
 
equals(other)
Returns true if the passed object is equal to this one.
 
getProperty(property)
Can be used instead of a getter by passing the name of the property you want as a string.
 

Returns a DOM element (by default a SPAN) that contains a textual representation of the requested property of this element.

 

Forces an object to load it's data from the server.

 
Sets properties on this object and creates getters/setters for accessing them Also used by child classes to replace IDs with references to the actual objects
 
registerListener($super, type, callback)
Methods borrowed from class bbq.lang.Watchable:
deRegisterListener, notifyListener, notifyListeners, registerOneTimeListener

Constructor Detail

bbq.domain.Entity(options)

Parameters:
Name Type Comment
options {Object}
options.data {Object} A set of key/value pairs to pre-populate the default object with

Method Detail

{boolean} dataLoaded()

Returns true if this Entity is fully loaded. An Entity is considered fully loaded if bbq.domain.Entity#processData has processed every field in the object returned by bbq.domain.Entity#getDefaultObject
Returns:
{boolean} True if this Entity is fully loaded.

{boolean} equals(other)

Returns true if the passed object is equal to this one. An object is considered equal if it is either equal (==) or implements a getId property and the output of which is equal (==) to the output of this object's getId method.
Parameters:
Name Type Comment
other {Object}
Returns:
{boolean} True if the passed object is considered equal to this one

{Object} getProperty(property)

Can be used instead of a getter by passing the name of the property you want as a string.
Parameters:
Name Type Comment
property {String} A property of this object
Returns:
{Object}

getPropertyDisplay(options)

Returns a DOM element (by default a SPAN) that contains a textual representation of the requested property of this element.

Will be updated automatically to contain the most recent value.

Supports getting properties of sub objects. For example, if we want the name of this objects's creator, we can do:


object.getPropertyDisplay({property: "creator.fullname"});

This will have the same effect as:


var creator = object.getCreator();
creator.getPropertyDisplay({property: "fullname"});

If the property requested has not yet been loaded, a call to Entity#loadData will occur.

Parameters:
Name Type Comment
options {Object}
options.property {String} The name of the property that is to be displayed
options.formatter
Optional
{Function} A function that takes the property value as an argument and returns a String or a Node
options.nodeName
Optional
{String} Will be used in place of SPAN
options.className
Optional
{String} Will be applied to the node
options.createNode
Optional
{Function} Should return a DOM node. Omit this to use a SPAN. If passed you should also pass a function for updateNode
options.updateNode
Optional
{Function} Expect two arguments - a node and the property value. Return nothing. If passed you should also pass a function for createNode

loadData()

Forces an object to load it's data from the server. If the object is not fully loaded an immediate request to _retrieveURL will be sent.

Register for listener "onDataLoaded" to interact with this object after data has been loaded.


processData(data)

Sets properties on this object and creates getters/setters for accessing them Also used by child classes to replace IDs with references to the actual objects
Parameters:
Name Type Comment
data {Object}

{String} registerListener($super, type, callback)

Parameters:
Name Type Comment
$super
type
callback
Returns:
{String}