HTML and CSS Reference
In-Depth Information
Batman server synchronization. A Batman.js model object may have arbitrary properties
set on it, just like any JS object. Only some of those properties are serialized and persisted
to its storage backends. Models have the ability to:
• Persist to various storage backends
• Only serialize a defined subset of their properties as JSON
• Use a state machine to expose life cycle events
• Validate with synchronous or asynchronous operations
You define persisted attributes on a model with the encode macro:
class Article extends
Batman . Model
@encode 'body_html' , 'title' , 'author' , 'summary_html' , 'blog_id' , 'id' , 'user_id'
@encode 'created_at' , 'updated_at' , 'published_at' , Batman . Encoders . railsDate
@encode 'tags' ,
encode: (tagSet) -> tagSet . toArray (). join ( ', ' )
decode: (tagString) -> new Batman . Set ( tagString . split ( ', ' )...)
Given one or more strings as arguments, @encode will register these properties as per‐
sisted attributes of the model, to be serialized in the model's toJSON() output and ex‐
tracted in its fromJSON() . Properties that aren't specified with @encode will be ignored
for both serialization and deserialization. If an optional coder object is provided as the
last argument, its encode and decode functions will be used by the Model for serializa‐
tion and deserialization, respectively.
By default, a model's primary key (the unchanging property that uniquely indexes its
instances) is its id property. If you want your model to have a different primary key,
specify the name of the key on the primaryKey class property:
class User extends
Batman . Model
@primaryKey: 'handle'
@encode 'handle' , 'email'
To specify a storage adapter for persisting a model, use the @persist macro in its class
definition:
class Product extends
Batman . Model
@persist Batman . LocalStorage
Now when you call save() or load() on a product, it will use the browser window's
localStorage to retrieve or store the serialized data.
If you have a REST backend you want to connect to, Batman.RestStorage is a simple
storage adapter that you can subclass and extend to suit your needs. By default, it assumes
Search WWH ::




Custom Search