3vot-model

3VOT Model based on SpineJS

clay-model

A Javascript Model Implementation with Promises based on SpineJS Model

Class methods

var User = Model.setup("User", [ "first_name", "last_name" ]);

Set up the model and its attributes. This is required for every model, and should be called before anything else is.

Bind event listeners to the model. These are executed in the context of the model.

User.bind("refresh change", function(user){ alert("#{user.name} changed!")) } See events for more information.

Trigger a custom event, see events for more information.

Unbind events, see the events guide for more information.

Find records by ID - returning the record instance. If the record doesn't exist, @notFound will be run unless a custom callback was also passed in.

var user = User.find("1")

Returns a boolean indicating if the record with the specified ID exists or not.

var user = User.exists("1")

Appends to all the stored records, without calling any create, update, save or destroy events. The only event that will be triggered is the refresh event. You can pass the option {clear: true} to wipe all the existing records. Internally @refresh calls fromJSON(), so you can also pass it JSON instead of an array.

User.refresh([{id: 1, name: "test"}, {id: 2, name: "test2"}])

Select all records that the callback function returns true to.

bobs = User.select(function(user){ return( user.name == "bob" ); }

Find the first record that has the given attribute & value.

bob = User.findByAttribute("name", "bob")

Find all records that have the given attribute & value.

bobs = User.findAllByAttribute("name", "bob")

Iterate over every record, passing it to the callback function.

User.each(function(user){ alert(user.name) });

Returns a cloned copy of every instance.

users = User.all()

Returns a cloned copies of instances from begin up to but not including end.

allUsersExceptFirst3 = User.slice(3) users7through13 = User.slice(6,13)

Returns a cloned copy of the first record. or an array of the first x records

Returns a cloned copy of the last record, or an array of the last x records

Returns the count of total records.

Deletes every record without triggering any events.

Destroys every record, triggering a destroy event on every record.

Updates the record with the matching ID, with the given attributes.

Creates a new record with the given attributes. Returns false if the record's validation fails, or the newly created record if successful.

Destroys the record with the given ID.

Utility function so the model has a valid JSON representation (shows all records).

Pass a JSON string, representing either an array or a singleton, to @fromJSON(). Returns an array or unsaved model instances.

Wrap a function in a proxy so it will always execute in the context of the model. This is a JavaScript compatibility feature, and shouldn't be used in CoffeeScript.

create = Model.proxy(Model.create)

Model.setup(name, [attributes...])

Alternative method for creating a new model class. This is a JavaScript compatibility feature, and shouldn't be used in CoffeeScript.

var User = Model.setup("User", ["first_name", "last_name"])

Boolean indicating if the record has been saved or not. Use isNew() instead.

Returns a boolean indicating if the record has been saved or not.

Returns a boolean indicating if the record has passed validation.

By default a noop. Override this to provide custom validation. Return a string, containing the error message, if the record isn't valid. For example:

Model.prototype.validate = function(){ "Name required" unless @name }

Load a set of properties in, setting attributes.

user = new User user.load(name: "Sir Bob") attributes()

Returns a hash of attributes to values.

Returns a boolean indicating if the other record is equal (i.e. same class and ID) as the current instance.

if(user.eql(anotherUser)) alert("Yah!")

Creates or updates the record, returning false if the record's validation fails, or self if the record saves successfully. During a save, the beforeSave, change and save events are triggered. Also the create or update events will be fired depending on whether the record was created/updated.

user = new User(name: "Sir Robin") user.save()

alert("#{user.id} was saved") user = User.find(user.id)

Sets a single attribute, saving the instance.

user = new User user.updateAttribute("name", "Green Knight")

Updates a record with the given attributes, saving the record.

user = User.create() user.updateAttributes(name: "Sir Galahad the Pure")

Destroys the record, removing it from the record store and triggering the destroy event.

user = User.create()

Destroy will also unbind event listeners for the model and objects it was listening to.

Returns a new unsaved record, with the same attributes as the current record, save the ID, which will be null.

user = User.create(name: "Sir Bedevere") dup = user.dup() assertEqual( dup.name, "Sir Bedevere" )

Returns a prototype clone of the record. This is used internally for Dynamic Records, and is probably not something you need to worry about.

Returns a prototype clone of the record. This is used internally for Dynamic Records, and is probably not something you need to worry about.

Reloads a record's attributes from its saved counterpart.

Returns the record's attributes. This is used for JSON serialization:

record = new User(name: "Sir Lancelot the Brave")

assertEqual( JSON.stringify(record), '{"id":"foo","name":"Sir Lancelot the Brave"}' )

$.post("/record.json", JSON.stringify(record))

Returns a string representation of the record. A utility function used to display the record in the console.

Returns a boolean indicating whether the record has saved. Similar to isNew(), but it actually checks the models record store.

Bind to an event specifically on this record. on is an available alias.

Trigger an event specifically on this record. This will propagate up to the model too.

Unbind all events, or just certain events (as a comma seperated list), or a specific callback for given events. off is an available alias

A JavaScript compatibility function, that will wrap the given function so that it's always executed in the context of the record.

Model comes with an interface for Ajax or other connectors. Define the connector after creating the model with

Model.ajax = AjaxConnector;

You can find a reference of a Connector in clay-model-vfr

Model.create( values, options ), modelInstance.save( options ) and modelInstance.destroy(options)

All take an options argument, here are some values

ignoreAjax: [true/false]: Will only make modifications locally without sending changes to the server.

Used to execute custom API Calls, is defined by Ajax Connector

User.api( arguments... );