0.10.8 • Published 10 years ago

sails-backbone-generator v0.10.8

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

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