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.
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
Model( name[, options, class_functions, prototype_functions]) -> Model
Generates a Model class with the given underscored name, options, and class and prototype functionality.
| Options | Default | Description |
|---|---|---|
| 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. |
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( 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( 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( attributes[, callback]) -> model instance
Updates an existing object.
new GeneratedClass(attributes) -> instance
Creates a new instance of the generated class with the given attributes.
model_instance.attributes(include_associations) -> {attributes}
Returns a hash of all the attributes as key : value pairs. You can optionally include associations.
model_instance.destroy(callback) -> Boolean
Calls a delete request on a resource. Returns true if destroyed succesfully, false if otherwise.
model_instance.init(attributes)
Init is called when a new object is created. This function is useful for overwriting constructor functionality.
model_instance.new_record() -> Boolean
Returns true if the record hasn't been saved, false if otherwise.
model_instance.reload(callback) -> model_instance
Reloads the instance from its resource.
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.
model_instance.set_attributes(attributes) -> attributes
Updates the objects attributes.
model_instance.updates_attributes( attributes[, callback]) -> attributes
Updates the objects attributes and saves the object.
model_instance.valid() -> Boolean
Returns true if the object has errors, false if otherwise.