Model

attributes, build, destroy (class), destroy (prototype), find, init, Model, new_record, reload, save, set_attributes, update, update_attributes, valid,

Model queries REST APIs like Rail's own ActiveResource. It's a convient way to perform Ajax calls on a REST API. Model is based of the wonderful Jester library.

Example

Model('todo', null, 
  {//Class Functions
    destroy: function(id, callback){
      var new_callback = function(result){
        alert(result ? 'Todo deleted successfully' : "The todo couldn't be deleted");
        callback(result);
      }
      this._super(id, new_callback);
    }
  },
  {//Prototype Functions
    save: function(){
      this.date = new Date();
      this._super();
    }
  }
);
find_callback = function(todos){
	alert('found '+todos.length+' todos');
}


var todos = Todo.find('all', find_callback)  //calls find_callback with an array of all todos
var first = Todo.find(2)      //returns the todo with ID = 2

Todo.destroy(2)               //deletes the todo with ID = 2
                              //and alerts if successful

var todo = new Todo({name: 'take out trash'}) //creates a new todo
todo.save()                   //saves the todo

Generator

Model

Model( name[, options, class_functions, prototype_functions]) -> Model

Generates a Model class with the given underscored name, options, and class and prototype functionality.

- Underscore name that gets camelized and capitialized into a the class name. For example: Model('my_class_name') becomes MyClassName
- An optional hash that configures the resource urls with the following optional values:
OptionsDefaultDescription
format xml The format of the response data. The other option is'json'.
prefix '' A string prefix added to the default urls. This is useful if your resource urls aren't immediately following your domain. For example, if your resources are in http://mydomain.com/resources/todos.xml, you would want your prefix to be 'resources'.
urls { list: "/name.format", show: "/name/:id.format", create: "/name.format", update: "/name/:id.format" } A hash of resource urls.
- Class functions for the model. You can call super functions with this._super( ... arguments ... )
- Prototype functions for instances of the model. You can overwrite inherited functions and call them with this._super( ... arguments ... )

Class Methods

build

create( attributes[, callback]) -> model instance

Creates a new model instance and creates a post request, creating the object. This is the same as using the constructor. Example:

Todo.build({name: 'take out trash', description: 'it stinks!'},callback)

destroy

destroy( id[, callback]) -> Boolean

Destroy's a record with the given id. Returns or calls back true if sucessfull, false if otherwise.

Todo.destroy(2,callback)

find

find( id[, params, callback]) -> null, model, or [models]

Finds and returns a single object or array of objects.

Todo.find(2);
Todo.find('first',{name: 'trash'}, callback);
Todo.find('all', null);

update

update( attributes[, callback]) -> model instance

Updates an existing object.

Constructor

new GeneratedClass(attributes) -> instance

Creates a new instance of the generated class with the given attributes.

Prototype Methods

attributes

model_instance.attributes(include_associations) -> {attributes}

Returns a hash of all the attributes as key : value pairs. You can optionally include associations.

destroy

model_instance.destroy(callback) -> Boolean

Calls a delete request on a resource. Returns true if destroyed succesfully, false if otherwise.

init

model_instance.init(attributes)

Init is called when a new object is created. This function is useful for overwriting constructor functionality.

new_record

model_instance.new_record() -> Boolean

Returns true if the record hasn't been saved, false if otherwise.

reload

model_instance.reload(callback) -> model_instance

Reloads the instance from its resource.

save

model_instance.save(callback) -> model_instance

Saves the object by performing a POST or PUT request depending if it is a new instance or not.

set_attributes

model_instance.set_attributes(attributes) -> attributes

Updates the objects attributes.

update_attributes

model_instance.updates_attributes( attributes[, callback]) -> attributes

Updates the objects attributes and saves the object.

valid

model_instance.valid() -> Boolean

Returns true if the object has errors, false if otherwise.