Documentation

include MVC MVC.Animate MVC.Class MVC.Controller MVC.Controller.Action MVC.Controller.Action.Drag MVC.Controller.Action.Drop MVC.Controller.Action.EnterLeave MVC.Controller.Action.Event MVC.Controller.Action.Hover MVC.Controller.Action.Lasso MVC.Controller.Action.Selectable MVC.Controller.Action.Subscribe MVC.Controller.Comet MVC.Controller.Params MVC.Controller.Params.Drag MVC.Controller.Params.Drop MVC.Controller.Stateful MVC.Delegator MVC.Doc MVC.Element MVC.Event MVC.File MVC.History MVC.IO MVC.IO.Ajax MVC.IO.Comet MVC.IO.JsonP MVC.IO.WindowName MVC.IO.XDoc MVC.Model MVC.Model.Ajax MVC.Model.Cookie MVC.Model.JsonP MVC.Model.JsonRest MVC.Model.WindowName MVC.Model.XmlRest MVC.Native MVC.Native.Array MVC.Native.Date MVC.Native.Function MVC.Native.Number MVC.Native.Object MVC.Native.String MVC.Options MVC.Store MVC.SyntheticEvent MVC.Test MVC.Test.Assertions MVC.Test.Controller MVC.Test.Functional MVC.Test.Runner MVC.Test.Unit MVC.Timer MVC.Timer.Easings MVC.Vector MVC.View MVC.View.Helpers OpenAjax

MVC.Model

Inherits: MVC.Class
Models wrap an application's data layer. This is done in two ways:
  • Requesting data from and interacting with services
  • Wrap service data with a domain-specific representation
A strong model layer keeps your code organized and maintainable, but it typically is the least developed part of the MVC architecture. This guide introduces you to the basics of how a model should work. Note that much of what it demonstrates could be done easier with a different base Model class such as Model.Ajax.

Services

Your models should be the way you communicate with the server. Instead of using Ajax/XHR requests directly, perform those requests in a model.

For example:
//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:
  • Group - operate on many instances. Ex: getting all tasks for a user.
  • Singular - operate on one instance. Ex: completing a task.
For these types of services, you will want to build them in slightly different ways.

Group Services

Group services that request data should look like the following:
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.

Singular Service

Singular services that minipulate data might look like:
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")
    }) })
  }
})

Wrapping Data

Now that you have instances, you can wrap their data in useful ways. This is done by adding functions to the Model's prototype methods. For example:
Task = MVC.Model.extend('task',
{},
{
  status : function(){
    return this.complete ? "COMPLETE" : "INCOMPLETE"
  }
})

P

  • Model.find_one(params, callbacks)
  • Model.find_all(params, callbacks)
  • Model.create(attributes, callbacks)
  • Model.update(id, attributes, callbacks)
  • Model.destroy(id, callbacks)


Using Stores

Model keeps all instances of a class in a Store. Stores provide an easy way of looking up instances by id.

Using OpenAjax



Static Methods

_clean_callbacks

_clean_callbacks(callbacks) -> undefined
Used for converting callbacks to to seperate failure and succcess
{Object} -

add_attribute

add_attribute(property, type) -> undefined
Adds an attribute to the list of attributes for this class.
{String} -
{String} -

callback

callback(fname) -> Function
Creates a callback function that will call back the function on the static class. If other arguments are passed, they will be added before the parameters used to call the callback.
{String} -
{Function} - a callback function useful for Ajax calls

create_as_existing

create_as_existing(attributes) -> Model
Used to create an existing object from attributes
{Object} -
{Model} - an instance of the model

create_many_as_existing

create_many_as_existing(instances) -> Array
Creates many instances
{Object} -
{Array} - an array of instances of the model

element_id_to_id

element_id_to_id(element_id) -> String
Takes an element ID like 'todo_5' and returns '5'
{Object} -
{String} -

find_by_element

find_by_element(el) -> undefined
Returns an instance if one can be found in the store.
{Object} -

id

The name of the id field. Defaults to 'id'

init

init(id, params, callbacks) -> Model
Finds objects in this class
{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

namespace

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

publish(event, data) -> undefined
Publishes to open ajax hub. Always adds the className.event
{Object} -
{Object} -

Prototype Methods

_clear

_clear() -> undefined
for (var attr in attributes){ if(attributes.hasOwnProperty(attr)){ this._setAttribute(attr, attributes[attr]); } }

_setProperty

_setProperty(property, value) -> undefined
Checks if there is a set_property value. If it returns true, lets it handle; otherwise saves it.
{Object} -
{Object} -

attributes

attributes() -> Object
Returns a list of attribues.
{Object} -

callback

callback(fname) -> Function
Creates a callback function that will call back the function on the instance. If other arguments are passed, they will be added before the parameters used to call the callback.
{String} -
{Function} - a callback function useful for Ajax calls

destroy

destroy(callback) -> undefined
Destroys the instance
{Function} - or object of callbacks

element

element() -> undefined
Returns the element found by using element_id for this instance

element_id

element_id() -> undefined
Returns the suggested element id for this instance

init

init(attributes) -> undefined
Creates, but does not save a new instance of this class
{Object} - -> a hash of attributes

is_new_record

is_new_record() -> undefined
Returns if the instance is a new object

publish

publish(event, data) -> undefined
Publishes to open ajax hub
{String} -
{Object} - if missing, uses the instance in {data: this}

save

save(callbacks) -> undefined
Saves the instance
{Function} - onComplete function or object of callbacks

set_attributes

set_attributes(attributes) -> undefined
Sets a hash of attributes for this instance
{Object} -

update_attributes

update_attributes(attributes, callback) -> undefined
Sets the attributes on this instance and calls save.
{Object} -
{Object} -

validate

validate() -> undefined
Validates this instance