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.Ajax

Inherits: MVC.Model
Model.Ajax makes it easy to write models that handle the request / reponse cycle of most Ajax driven applications. It helps you escape transporting callbacks.
It uses convention to tie function names to resouces/ urls. This is best shown with an example: First, lets say we wanted an FTP model to be able to requests data from /ftp/dir. We could create that model like this:

Ftp = MVC.Model.Ajax.extend('ftp',
{
   dir_get_success: function(transport){
       return transport.responseText;
   }
},{})
You could make a request to /ftp/dir like this:
callback = function(data){alert(data)};
Ftp.dir({path: "/"}, callback)
There are a few things to notice here.
  • Ftp.dir function expects params as its first argument. These will post path="/" to /ftp/dir
  • There are no callbacks in the Ftp model. You return the data you want passed to the callback in dir_get_success
  • dir_get_success is called with the result of the transport.
  • The get makes the request use the HTTP verb get. You can use put/post/get/delete. If no verb is present, it defaults to post.


Model.Ajax allows you to expand on this pattern in several ways:

Changing the request path

If /ftp/dir was actually ftp_directory, you can change the directory name by adding the following:
Ftp = MVC.Model.Ajax.extend('ftp',
{
   dir_get_url: '/ftp_directory'
   dir_get_success: function(transport){
       return transport.responseText;
   }
},{})
dir_get_url could also be a function that dynamically returns the url. For example,
...
   dir_get_url: function(params){
       return "/ftp/dir/"+encodeURIComponent(params.path)
   }
...
would return "/ftp/dir/%2F" for Ftp.dir({path: "/"}, callback)

Customizing and validating the request

You can completely customize the request by making a function like X_request. Where X matches one of your success functions. The following example validates that params must include a path.
...
   dir_get_request: function(params){
       if(!params.path) throw "Path does not exist!"
   }
...
In a request function, you can also completely customize the request with the temporary this.request function. Example:
...
   dir_get_request: function(params){
       if(!params.path) throw "Path does not exist!"
       this.request("/ftp/dir/"+encodeURIComponent(params.path)+".json")
   }
...

Respond to errors

Finally, you can respond to Transport errors in the same way you respond to success:
Ftp = MVC.Model.Ajax.extend('ftp',
{
   dir_get_success: function(transport){
       return transport.responseText;
   },
   dir_get_failure: function(transport){
       return "error data"
   }
},{})

Static Methods

get_id

get_id(transport) -> undefined
Gets the id of a response from the responseHeader. This is commonly used in REST based calls
{Object} -

init

init() -> undefined
Goes through the list of static functions. If they end with _success, creates a requesting function for them.

json_from_string

json_from_string(json_string) -> Object
overwrite this function if you don't want to eval js
{Object} - json string
{Object} - json converted to data

request

request(url, request_params, options) -> undefined
Request is only available inside generated request functions. If the first argument is not a string, it assumes the first 2 arguments are the request params and the options. If there are more than 3 arguments, those arguments will be used when calling back the success or failure functions.
{Object} -
{Object} - parameters passed to the request
{Object} - options sent to the transport