Skip to content

coroutine/crom

Repository files navigation

Crom

A JS library that laughs at your four winds.

Crom!

Example

Server Response

Below is our example JSON response from the RESTful API endpoint http://example.com/snakepits/1.json:

{
  "depth": 40,
  "units": "meters",
  "owner": {
    "name": "Crom",
    "title": "God of Steel"
  },
  "snakes": [
    {
      "length": "20",
      "units":  "meters",
      "isVenomous": true,
      "likes": "killing the shit out of people."
    },
    {
      "length": "12",
      "units":  "meters",
      "isVenomous": false,
      "likes": "long walks on the beach."
    }
  ]
}

Crom Models

And now, the corresponding Backbone Models, leveraging Crom:

Person

class Person extends Crom.Model
  defaults:
    name: null
    title: null

Snake

class Snake extends Crom.Model
  defaults:
    length: 0
    units: null
    isVenomous: false
    likes: null

Snakes (Collection)

class Snakes extends Crom.Collection
  model: Snake

Snakepit

class Snakepit extends Crom.Model

  # our glorious RESTful endpoint
  url: 'http://example.com/snakepits'

  defaults:
    depth: 0
    units: null

  # Here we have some nested data
  nested: ->
    owner:  Person
    snakes: Snakes

Putting it all together

Now we're able to put our models to the test:

snakepit = new Snakepit(id: 1)
snakepit.fetch()

snakepit.owner # -> an instance of the Person model
snakepit.snakes # -> an instance of the Snakes collection

Releases

No releases published

Packages

No packages published