Code Monkey home page Code Monkey logo

yayson's Introduction

YAYSON

A library for serializing and reading JSON API data in JavaScript. As of 2.0.0 YAYSON aims to support JSON API version 1.

Installing

Install yayson by running:

$ npm install yayson --save

Presenting data

A basic Presenter can look like this:

  {Presenter} = require('yayson')(adapter: 'default')

  class ItemsPresenter extends Presenter
    type: 'items'

  item =
    id: 5
    name: 'First'

  ItemsPresenter.render(item)

This would produce:

{
  data: {
    id: 5,
    type: 'items',
    attributes: {
      id: 5,
      name: 'First'
    }
  }
}

It also works with arrays, so if you send an array to render, "data" will be an array.

A bit more advanced example:

  {Presenter} = require('yayson')(adapter: 'default')

  class ItemsPresenter extends Presenter
    type: 'items'

    attributes: ->
      attrs = super
      attrs.start = moment.utc(attrs.start).toDate()
      attrs

    relationships: ->
      event: EventsPresenter

  ItemsPresenter.render(item)

In JavaScript this would be done as:

var Presenter = require('yayson')().Presenter;

var ItemsPresenter = function () { Presenter.call(this); }
ItemsPresenter.prototype = new Presenter();

ItemsPresenter.prototype.type = 'items'

ItemsPresenter.prototype.attributes = function() {
  var attrs = Presenter.prototype.attributes.apply(this, arguments);

  attrs.start = moment.utc(attrs.start).toDate();
  return attrs;
}

ItemsPresenter.prototype.relationships = function() {
  return {
    event: EventsPresenter
  }
}

ItemsPresenter.render(item)

Sequelize support

By default it is set up to handle standard JS objects. You can also make it handle Sequelize.js models like this:

{Presenter} = require('yayson')(adapter: 'sequelize')

Take a look at the SequelizeAdapter if you want to extend YAYSON to your ORM. Pull requests are welcome. :)

Metadata

You can add metadata to the top level object.

  ItemsPresenter.render(items, meta: count: 10)

This would produce:

{
  meta: {
    count: 10
  }
  data: {
    id: 5,
    type: 'items',
    attributes: {
      id: 5,
      name: 'First'
    }
  }
}

Parsing data

You can use a Store can like this:

    {Store} = require('yayson')()
    store = new Store()

    adapter.get(path: '/events/' + id).then (data) ->
      event = store.sync(data)

This will give you the parsed event with all its relationships.

Use in the browser

Recommended way is to use it via webpack or similar build system wich lets you just require the package as usual.

If you just want to try it out, copy the file dist/yayson.js to your project. Then simply include it:

    <script src="./lib/yayson.js"></script>

Then you can var yayson = window.yayson() use the yayson.Presenter and yayson.Store as usual.

Browser support

Tested

  • Chrome
  • Firefox
  • Safari
  • Safari iOS

Untested, but should work

  • IE 9+
  • Android

yayson's People

Contributors

jede avatar ajcobo avatar interlock avatar javve avatar lsanwick avatar dmtrs avatar andresberrios avatar andresgottlieb avatar antonaut avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.