Models with js-model

10 Mar 2010 | By Alex Young | Tags libraries functional

js-model by Ben Pickles is a client-side library for working with the model layer. It’s not simply a REST proxy, it supports features like data caching, validation, and models can be hooked up to the UI using events.

Validations look like this:

var Post = Model('post', {
  validate: function() {
    if (this.attr("title") != "Bar") {
      this.errors.add("title", "should be Bar")
    }
  }
})

var post = new Post()
post.attr("title", "Foo")

post.valid()                // => false
post.errors.size()          // => 1
post.errors.on("title")     // => ["should be Bar"]
post.errors.all()           // => { title: ["should be Bar"] }

post.attr("title", "Bar")

post.valid()                // => true

REST can be used to persist data, but you could technically add your own persistence layer:

var Project = Model("project", {
  persistence: Model.RestPersistence("/projects")
})

This project requires jQuery and Underscore.


blog comments powered by Disqus