Modelize is a small model/data interface for REST APIs using KnockoutJS. It also integrates encryption functionality in combination with SJCL.
Page setup first, include all required scripts:
<!-- jQuery --><scriptsrc="/vendor/jquery/dist/jquery.min.js"></script><!-- jQuery rest --><scriptsrc="/vendor/jquery.rest/dist/1/jquery.rest.min.js"></script><!-- Knockout --><scriptsrc="/vendor/knockoutjs/dist/knockout.js"></script><!-- SJCL (optional) --><scriptsrc="/vendor/sjcl/sjcl.js"></script><!-- Modelize --><scriptsrc="/vendor/modelize/dist/modelize.min.js"></script>Setup a connector:
api_connector=newRESTConnector('/api/')Then define some models in coffeescript:
# Post model with a simple 1:n relation for comments and two editable data fieldsPost= Modelize
api:'posts'connector: api_connector
has_many:comment:model:'Comment'editable: [
'title',
'content' ]
# The corresponding comment modelComment= 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_connectorUsing your models:
# Get all (published e.g.) posts and put them in the observable array @postsPost.get { status:'published' }, @posts# You can also define custom methods for retrievalPost.get { id:1 }, (post) =>console.log posthas_one relations add a relation_id field to the main model.
relation()relation_get([parameters], [callbackOrObservable])relations()
# For Example:Post.comments()has_many relation access is always extended with an 's'. So relation 'comment', turns to 'comments'
relation_get([parameters], [callbackOrObservable])
# For Example:Post.comment_get()
# OrPost.comment_get({ status:'not_spam' })relation_add([parameters], [callbackOrObservable])relation_destroy([callback])editablefunctionsobservablecomputedpurecomputedcreate(callback)update(params, callback)export([id])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.
MetaInformation= Container
editable: [
'title',
'description'
]Post= Modelize
container:MetaInformation:first_class:truedatahandler: [object, default: instance of JSONHandler]first_class: [bool, default:false]If this is set to true, then all editables are mapped to the main model object and available for direct editing.
container: [string, default:containeroption name]field: [string, default:containeroption name]