Giter Site home page Giter Site logo

model's Introduction

model

W.I.P minimalistic extensible model component.

API

model(name)

Create a new model with the given name.

var model = require('model');
var User = model('User');

.attr(name, [meta])

Define an attribute name with optional meta data object.

var model = require('model');

var Post = model('Post')
  .attr('id')
  .attr('title')
  .attr('body')
  .attr('created_at')
  .attr('updated_at')

With meta data used by plugins:

var model = require('model');

var Post = model('Post')
  .attr('id', { required: true, type: 'number' })
  .attr('title', { required: true, type: 'string' })
  .attr('body', { required: true, type: 'string' })
  .attr('created_at', { type: 'date' })
  .attr('updated_at', { type: 'date' })

.validate(fn)

TODO: validation callback docs

.use(fn)

TODO: plugin docs

.url([path])

Return base url, or url to path.

User.url()
// => "/users"

User.url('add')
// => "/users/add"

#ATTR()

"Getter" function generated when Model.attr(name) is called.

var Post = model('Post')
  .attr('title')
  .attr('body')

var post = new Post;
post.title('Ferrets')
post.body('Make really good pets')

#ATTR(value)

"Setter" function generated when Model.attr(name) is called.

var Post = model('Post')
  .attr('title')
  .attr('body')

var post = new Post({ title: 'Cats' });

post.title()
// => "Cats"

post.title('Ferrets')
post.title()
// => "Ferrets"
  • Emits "change" event with (name, value, previousValue).
  • Emits "change ATTR" event with (value, previousValue).
post.on('change', function(name, val, prev){
  console.log('changed %s from %s to %s', name, prev, val)
})

post.on('change title', function(val, prev){
  console.log('changed title')
})

#isNew()

Returns true if the model is unsaved.

#toJSON()

Return a JSON representation of the model (its attributes).

#has(attr)

Check if attr is non-null.

#get(attr)

Get attr's value.

#set(attrs)

Set multiple attrs.

user.set({ name: 'Tobi', age: 2 })

#changed([attr])

Check if the model is "dirty" and return an object of changed attributes. Optionally check a specific attr and return a Boolean.

#error(attr, msg)

Define error msg for attr.

#isValid()

Run validations and check if the model is valid.

user.isValid()
// => false

user.errors
// => [{ attr: ..., message: ... }]

#url([path])

Return this model's base url or relative to path:

var user = new User({ id: 5 });
user.url('edit');
// => "/users/5/edit"

#save(fn)

Save or update and invoke the given callback fn(err).

var user = new User({ name: 'Tobi' })

user.save(function(err){
  
})

Emits "save" when complete.

#destroy([fn])

Destroy and invoke optional fn(err).

Emits "destroy" when successfully deleted.

Links

License

MIT

model's People

Contributors

tj avatar matthewmueller avatar

Watchers

Peter Swan avatar Ethan Fuchs avatar 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.