Class: Wraith.Model

Defined in: src/model.coffee
Inherits: Wraith.Base

Overview

Our data model used throughout the application. Currently it is requred to do any data-binding or data-mapping in the views. Credit goes out to Alex MacCaw and his Spine framework for very obvious inspiration.

Class Method Summary

Instance Method Summary

Inherited Method Summary

Methods inherited from Wraith.Base

#constructor, #bind, #unbind, #emit, #proxy

Class Method Details

+ (void) field(name, opt)

Sets up a field with the given name and options.

Parameters:

  • name (String) The name of the field to setup
  • opt (Object) The options object (optional). Used to setup defaults.

+ (void) hasMany(klass, as, opt)

Sets up a collection given the Wraith.Collection object and options object.

Parameters:

  • klass (Wraith.Collection) The collection object to use.
  • as (String) The name of the collection within this model.
  • opt (Object) The options to apply to this collection.

Constructor Details

- (void) constructor(attributes)

Constructor

Parameters:

  • attributes (Object) An attributes object to apply to the model upon intialization.

Instance Method Details

- (void) reset(attributes)

Perform a reset of the models attributes. Will trigger "change" events on each property that is reset.

Parameters:

  • attributes (Object) Contains all default data to be applied to the object when being reset.

- (Object, String, Boolean, Number) get(key) (bound)

Returns the value for the given key. Will return undefined if not found on the attributes list.

Parameters:

  • key (String) The key to look up.

Returns:

  • (Object, String, Boolean, Number) — The value stored at attributes.key

- (void) set(key, val) (bound)

Sets the given attributes.key value to val. Warning: if the key is not found on attributes, it will throw an error.

@params [String] key The key of the attributes object to set. @params [Object, String, Boolean, Number] val The value to update the given key to.

- (Boolean) isValid() (bound)

Checks to see if there are any errors on the models. An error cache is stored privately so its as easy as checking if there is anything in that object.

Returns:

  • (Boolean) — If the model is valid or not

- (Object) errors() (bound)

Returns an object with errors stored on it. Errors are stored with key as the attribute name and the value as the error.

Returns:

  • (Object) — The associative array of errors

- (Object) toJSON() (bound)

"Serializes" the model's attributes as JSON. (Really just returns the attributes object)

Returns:

  • (Object) — The attributes object belonging to this Wraith.Model instance.