Modelize
Modelize is a small model/data interface for REST APIs using KnockoutJS. It also integrates encryption functionality in combination with SJCL.
Basic Usage
Page setup first, include all required scripts:
<!-- jQuery --><!-- jQuery rest --><!-- Knockout --><!-- SJCL (optional) --><!-- Modelize -->
Setup a connector:
api_connector = '/api/'
Then define some models in coffeescript:
# Post model with a simple 1:n relation for comments and two editable data fields Post = Modelize api: 'posts' connector: api_connector has_many: comment: model: 'Comment' editable: 'title' 'content' # The corresponding comment model Comment = Modelize api: 'posts' connector: api_connector belongs_to: post: model: 'Post' editable: 'content'
But basically the model can be as short as just this:
Model = Modelize api: 'stuff' connector: api_connector
Using your models:
# Get all (published e.g.) posts and put them in the observable array @posts Postget status: 'published' @posts# You can also define custom methods for retrieval Postget id: 1 consolelog post
Docs
Relations
has_one and belongs_to relations
has_one relations add a relation_id
field to the main model.
relation
relation_getparameterscallbackOrObservable
has_many relations
relations# For Example: Postcomments
has_many relation access is always extended with an 's'. So relation 'comment', turns to 'comments'
relation_getparameterscallbackOrObservable# For Example: Postcomment_get# Or Postcomment_get status: 'not_spam'
relation_addparameterscallbackOrObservable
relation_destroycallback
Observable types
editable
functions
observable
computed
purecomputed
Predefined functions
createcallback
updateparamscallback
exportid
Containers
Containers are defined just like the main models, with the exception that they only support the observable types and don't have any api/connector or relational options.
Definition
MetaInformation = Container editable: 'title' 'description'
Usage and options
Post = Modelize container: MetaInformation: first_class: true
datahandler: objectdefault: instance of JSONHandler
first_class: booldefault: false
If this is set to true, then all editables are mapped to the main model object and available for direct editing.
container: stringdefault: container option name
field: stringdefault: container option name