Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Yet Another Javascript Model
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
spec
.gitignore
.jsbeautifyrc
.jshintrc
Gruntfile.js
README.md
index.js
package.json

README.md

Yet Another Javascript Model

Javascript Domain Model factory.

Install

npm install yajm --save

Usage

var Model = require('yajm').Model;
var Types = require('yajm').Types;


// define a model class
var User = Model.define({
  id: Types.Integer,

  username: String,

  isAdmin: {
    type: Boolean,
    default: false
  },

  // array of string
  tags: [String],

  // array of anonymous contact model
  contacts: [{
    firstName: String,
    lastName: String,
    email: String
  }]

});

// create a user
var user = new User({
  id: '123',
  tags: ['foo', 'bar'],
  contacts: [{
    firstName: 'aaron',
    lastName: 'qian',
    email: 'aq@gmail'
  }]
});

// get attribute
user.id; // 123, note this is already been coreced into integer
user.tags; // ['foo', 'bar']

// set attribute and coersion
user.id = "456";
user.id; // 456
user.isAdmin = 'yes';
user.isAdmin // true

// setting undefined property:
user.undefinedProp = "foo";
// In strict mode, causes TypeError to be thrown.
// In non-strict mode, silently ignored.

// Silently ignored in either modes.
user._set('undefinedProp', 'foo');
Something went wrong with that request. Please try again.