Fork me on GitHub

AFrameJS

- Javascript MVC Library

class AFrame.Model class extends AFrame.DataContainer

A Model is a DataContainer that is associated with a Schema. If no initial data is given, default values will be retreived from the schema. When doing a set, only data that validates will be set. If data to set is invalid, set will return a FieldValidityState.

// create the schema config
var noteSchemaConfig = {
    id: { type: 'integer' },
    title: { type: 'text', def: 'Note Title' },
    contents: { type: 'text' },
    date: { type: 'iso8601' },
    edit_date: { type: 'iso8601' }
};

// Create A Model Class
var ModelClass = AFrame.Model.extend( {
    schema: noteSchemaConfig
} );

// Create an instance of ModelClass
var model = ModelClass.create( {
    data: {
       id: '1',
       title: 'Get some milk',
       contents: 'Go to the supermarket and grab some milk.',
       date: '2010-12-10T18:09Z',
       edit_date: '2010-12-10T18:23Z'
       extra_field: 'this field does not get through'
    }
} );

// update a field.  prevVal will be 'Get some milk'
var prevVal = model.set( 'title', 'Get some milk and eggs' );

// This is setting the date in error, the prevVal will have a
// FieldValidityState with its typeMismatch field set to true.
// This will NOT actually set the value.
prevVal = model.set( 'edit_date', '1' );

// Check the overall model for validity.  Returns true if all valid, an
// object of FieldValidityStates otherwise
var isValid = model.checkValidity();

Manual creation of a Model

It is also possible to create a model instance by creating an instance of AFrame.Model and associating it with a schemaConfig.

// Manually create a model
var model = AFrame.Model.create( {
    schema: noteSchemaConfig,
    data: {
        // data here
    }
} );

Constructor

Constructor Parameters Returns
AFrame.Model( )

Methods

Methods Returns Description
checkValidity( fieldName, fieldValue ) variant

Check the validity of the potential value of a field

var retval = model.checkValidity( 'name', 'Shane Tomlinson' );
if( retval !== true ) {
    // something went wrong, value would be invalid.
}

Parameters:

  • fieldName <string>

    name of field

  • fieldValue <variant>

    potential value of field

set( fieldName, fieldValue, force ) variant

Set an item of data. Model will only be updated if data validates or force is set to true. If data validates, the previous value will be returned. If data does not validate, a FieldValidityState will be returned.

// update single item
var retval = model.set( 'name', 'Shane Tomlinson' );
if( retval !== true ) {
    // something went wrong
}

// bulk update.  retVals will have a true/FieldValidityState for
// each item being set.
var retVals = model.set( {
    name: 'Shane Tomlinson',
    employer: 'AFrame Foundary'
} );

Parameters:

  • fieldName <string>

    name of field

  • fieldValue <variant>

    value of field

  • force <boolean>

    force update

    • Returns:

toSerializedJSON( ) object

Get an object suitable to send to persistence. This is based roughly on converting the data to a FormData "like" object - see MDC All items in the schema that do not have save parameter set to false and have values defined in dataToSerialize will have values returned.

 // Get an object suitable to send to persistence.
 var serializedData = model.toSerializedJSON();

Events inherited from AFrame.AObject

Events Notes
onInit
onTeardown

Events inherited from AFrame.DataContainer

Events Notes
onSet
onSet-fieldName

Configuration Attributes

Attributes Type Description
data {object}

Initial data to use for the model. Note, initial data is not validated in any way. If data is not given, data is taken out of the schema's default values.

Configuration Attributes inherited from AFrame.AObject

Attributes Notes
{cid} cid

Configuration Attributes inherited from AFrame.DataContainer

Attributes Notes
data