Class Protobone.Model
Extends: Protobone.BaseClass 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
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.
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'
true when non of the listeners returned false, false if they do so.
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.
The value of the requested attribute, or an object with all attributes
getId
(
)
src/Model.js:81
Returns the instance's ID of the model. Null means it is a new, not saved instance.
The ID (int, string), if any
hasAttribute
( key
)
src/Model.js:307
Checks if the model has a certain attribute.
Parameters:-
key
<String>The attribute name to check
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: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: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 rootProperty
is 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.
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.
Supports fluent interface by returning itself
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)
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
)
-
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.
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 '
/ '
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
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.