JSPM

  • Created
  • Published
  • Downloads 115
  • Score
    100M100P100Q66003F
  • License MIT

Generate client-side Backbone.js Models from a Sails.js API. Uses Backbone Relational to generate relations, and Backbone Validator to generate validation functions.

Package Exports

  • sails-backbone

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sails-backbone) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sails-backbone-generator

Build Status

Generate client-side Backbone.js Models from a Sails.js API. Uses Backbone Relational to generate relations, and implements validation rules using Anchor, the same library used to validate Sails.js models attributes.

Install

$ npm install sails-backbone-generator --save

Usage

Sails.js (Server)

Model

// Automobile.js
module.exports = {
  attributes: {
    name: {
      type: 'string',
      alphanum: true,
      primaryKey: true
    },
    color: {
      type: 'string',
      enum: [ 'black', 'black' ]
    }
  }
};

// Truck.js
_.merge(module.exports, require('./Automobile'), {
  extend: 'Automobile',
  attributes: {
    bedlength: {
      type: 'integer'
    }
  }
};

Controller

var SailsBackbone = require('sails-backbone-generator');
var schema = SailsBackbone.generate(sails);
res.json(schema);

Browser (Client)

app.models = SailsBackbone.parse(schema);
Backbone.Relational.store.addModelScope(app.models);

Translated into Backbone:

xm.Automobile = Backbone.RelationalModel.extend({
  idAttribute: 'name',
  urlRoot: 'http://example.com/automobile',
  validations: {
    name: {
      alphanum: true
    }
  }
});

xm.Truck = xm.Automobile.extend({
  idAttribute: 'name',
  urlRoot: 'http://example.com/truck',
  validations: {
    name: {
      alphanum: true
    },
    bedlength: {
      integer: true
    }
  }
});

More