//instead of:
new Ajax('/tasks.json', {onComplete: find_tasks_next_week })
//do this:
Task.find('all', find_tasks_next_week)
//instead of
new Ajax('/tasks/'+id+'/complete.json',{onComplete: task_completed})
//do this:
task.complete(task_completed)Typically there are two types of services any application connects to:
Task = MVC.Model.extend('task',
{
find : function(params, callback){
new Ajax('/tasks.json', {onComplete: MVC.Function.bind(function(response){
//get data into the right format for create_as_existing
var data = eval('('+json_string+')');
//call create_as_existing to create instances
var instances = this.create_many_as_existing(data);
//call back with data.
callback(instances)
}) })
}
},
{})Note this function uses create_many_as_existing
to create new instances. By using create_many_as_existing, the model will also publish
OpenAjax.hub messages that can be listed to by controllers.
Task = MVC.Model.extend('task',
{},
{
complete: function(callback){
new Ajax('/tasks/'+this.id+'/complete.json', {onComplete: MVC.Function.bind(function(response){
this.completed = true;
callback(this)
this.publish("completed")
}) })
}
})Task = MVC.Model.extend('task',
{},
{
status : function(){
return this.complete ? "COMPLETE" : "INCOMPLETE"
}
})_clean_callbacks(callbacks) -> undefined
{Object} -
add_attribute(property, type) -> undefined
{String} -
{String} -
callback(fname) -> Function
{String} -
{Function} - a callback function useful for Ajax calls
create_as_existing(attributes) -> Model
{Object} -
{Model} - an instance of the model
create_many_as_existing(instances) -> Array
{Object} -
{Array} - an array of instances of the model
element_id_to_id(element_id) -> String
{Object} -
{String} -
find_by_element(el) -> undefined
{Object} -
The name of the id field. Defaults to 'id'
init(id, params, callbacks) -> Model
{Object} - the id of a object
{Object} - params passed to the
{Object} - a single onComplete callback or a hash of callbacks
{Model} - will return instances of the model if syncronous
Namespaces are used to publish messages to a specific namespace. @code_start Org.Task = MVC.Model.extend('task',{ namespace: "org" }, { update: function(){ this.publish("update") // publishes 'this' to 'org.task.update' } }) @code_end
publish(event, data) -> undefined
{Object} -
{Object} -
_clear() -> undefined
_setProperty(property, value) -> undefined
{Object} -
{Object} -
attributes() -> Object
{Object} -
callback(fname) -> Function
{String} -
{Function} - a callback function useful for Ajax calls
destroy(callback) -> undefined
{Function} - or object of callbacks
element() -> undefined
element_id() -> undefined
init(attributes) -> undefined
{Object} - -> a hash of attributes
is_new_record() -> undefined
publish(event, data) -> undefined
{String} -
{Object} - if missing, uses the instance in {data: this}
save(callbacks) -> undefined
{Function} - onComplete function or object of callbacks
set_attributes(attributes) -> undefined
{Object} -
update_attributes(attributes, callback) -> undefined
{Object} -
{Object} -
validate() -> undefined