API Docs for: 0.0.6

Class Protobone.Model

Extends: Protobone.Base
Class defined in: src/statics.js:1

PrototypeJS Model extension - Enables Prototype JS users to fetch / store Models from / to a backend using AJAX / REST

Inspired by (but not copied) Backbone's Backbone.Model and Backbone.sync

Protobone.Model( )
src/statics.js:1
constructor( data )
src/Model.js:63

Constructor. Sets the given data (key/value pairs) as attributes on new model instances.

Parameters:
  • data <Object>

    Initial data (key/value pairs) to set on the new Model instance, e.g.: {name: 'Alex',age: 26}

destroy( options )
src/Model.js:237

invokes a delete request to the server. Only allowed for existing (id <> null) models. options is passed along to Prototype's Ajax.Request function.

After the deletion was successful, the model instance is updated with the server data, even if the server removed the instance.

Parameters:
  • options <Object>

    Additional Ajax options to be sent to Ajax.Request.

fetch( options )
src/Model.js:220

Fetches this Model's representation from the server. Only allowed for existing (id <> null) models. options is passed along to Prototype's Ajax.Request function.

Parameters:
  • options <Object>

    Additional Ajax options to be sent to Ajax.Request.

Boolean fireEvent( eventName )
Defined in Protobone.Base: src/Base.js:60

Fires an event, informing all listneners that are registered for the given event name. The fireEvent function can be called with any number of additional arguments, which are then passed to the event handler function.

Returns true if NONE of the registered handlers return false: As soon as one listener returns false, fireEvent will also return false.

Parameters:
  • eventName <String>

    The event to fire, e.g. 'updated'


Returns: <Boolean>

true when non of the listeners returned false, false if they do so.

Mixed get( key )
src/Model.js:157

Returns a specific attribute, or all if key is omitted

Parameters:
  • key <String>

    The name of the attribute to get. If omitted, an object containing all attributes (key/value) is returned.


Returns: <Mixed>

The value of the requested attribute, or an object with all attributes

Mixed getId( )
src/Model.js:81

Returns the instance's ID of the model. Null means it is a new, not saved instance.


Returns: <Mixed>

The ID (int, string), if any

Boolean hasAttribute( key )
src/Model.js:307

Checks if the model has a certain attribute.

Parameters:
  • key <String>

    The attribute name to check


Returns: <Boolean>
off( eventName, callback, this )
Defined in Protobone.Base: src/Base.js:34

Removes a specific event handler for an event, or removes all listerners from an event.

Parameters:
  • eventName <String>

    E.g. 'updated'

  • callback <Function>

    The callback to remove. If omitted, all callbacks for a specific event are removed

  • this <Boolean>
on( eventName, callback, this )
Defined in Protobone.Base: src/Base.js:16

Registers an event handler. It does not check on duplicity, so you can add the same event handler multiple times.

Parameters:
  • eventName <String>

    The name of the event, e.g. 'updated'

  • callback <Function>

    The listener function, called with event-specific {parameters}

  • this <Boolean>
parse( )
src/Model.js:289

Called by save() and fetch() with the server's data response. Fills in the server response to the model. In the default implementation, it just takes the plain JSON object from the server (if any) and store the values on the model. if rootPropertyis set, the data entry point is set to the rootProperty data.

save( options )
src/Model.js:197

Makes this model persistent by sending the data to a REST interface (by default). Make sure to set the urlRool property on class definition.

options are all options that Prototype's Ajax.Request understands, so you can e.g. deliver a onSuccess callback:

myModel.save({onSuccess: function(response,model){
    // do something after save here
}});
Parameters:
  • options <Object>

    Additional Ajax options to be sent to Ajax.Request.

This set( /Object, value )
src/Model.js:106

Sets Model attributes (key/values). Takes either a key and a value, or a plain object containing key/value pairs.

Parameters:
  • /Object <String>

    keyOrObject A string representing the key (e.g. 'name') or an object with key/values (e.g. 'name':'alex','age':'too old')

  • value <Mixed>

    The value to set if keyOrObject is a string. Ignored when keyOrObject is an object.


Returns: <This>

Supports fluent interface by returning itself

This setId( id )
src/Model.js:92

Sets the Model instance's ID. It also sets it as attribute value so that it is sent to the server when synced.

Parameters:
  • id <Mixed>

    The id to set (e.g. an integer, or even a string)


Returns: <This>

Supports fluent interface by returning itself

sync( url, method, model, options )
src/statics.js:61

This method is where the real server communication happens. It uses Prototype's Ajax.Request to send / load data to/from a REST backend. It is called in a static context (Protobone.Model.sync) from a Model's instance and can be overridden if you have to implement your own storage backend (e.g. localstore) or an incompatible API.

The default method uses the following HTTP methods

  • method: create -> POST /collection
  • method: read -> GET /collection[/id]
  • method: update -> POST /collection/id (Request Header: X-HTTP-Method-Override: put)
  • method: delete -> POST /collection/id (Request Header: X-HTTP-Method-Override: delete)
Parameters:
  • url <String>

    The URL for the request

  • method <String>

    The HTTP method. Only supported at the moment: 'get', 'post' (as of Prototype's limitation)

  • model <Protobone.Model>

    A model instance to be sent / updated to /from the server

  • options <Object>

    Additional Ajax.Request options

sync( )
src/Model.js:278

Just calls Protobone.Model.sync. If you want your own, Model-specific implementation, override this function.

String url( )
src/Model.js:176

Creates the REST url for the actual state of the Model. Override this method if you want to implement your own URL scheme. Here is how it works by default:

  • non-persistent state (id = null): return ''
  • persistent state (id <> null): return '/'

Returns: <String>

The URL for this Model instance, e.g. /root/Entity/3

emulateHTTP <Boolean>
src/statics.js:39

If set to true, only use GET (read) and POST (create,update,delete) HTTP Methods, and set the X-HTTP-Method-Override request header with the true method.

NOTE: TODO: At the moment, only legacy methods (GET/POST) are supported (emulateHTTP: true), because the Prototype JS library does NOT support other requests than POST/GET. So set emulateHTTP to false does not change a thing, unfortunately...

emulateJSON <Boolean>
src/statics.js:53

TODO: Implement emulateJSON to use a post body instead of RAW json

id <Unknown>
src/Model.js:36

Defines the name of the ID attribute. Defaults to id.

rootProperty <Unknown>
src/Model.js:53

Used by the parse() function, it defines the root property in the server's json response which contains the payload data for the model. Defaults to null (delivered json directly contains model attributes)

urlRoot <String>
src/Model.js:43

The URL root for this Model. Must be set in child classes, e.g. to '/entities/Person'. Used by the url() function to build the persistence URL.