Coder Social home page Coder Social logo

kristianmandrup / tubbs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dandean/tubbs

0.0 3.0 0.0 246 KB

Mr. Tubbs was a Nottinghamshire gentleman who developed a most unfortunate obsession with fairies.

Home Page: https://github.com/dandean/tubbs

JavaScript 100.00%

tubbs's Introduction

Tubbs

Build Status

Tubbs is Data Model Layer which makes working with your data much easier.

Features

  • ActiveModel-style validation
  • Observe property value changes
  • Observe deletion and save
  • Abstract data store interface. So far:
    • In-memory (built-in: Tubbs.MemoryStore)
    • Riak (via tubbs-riakstorage - server-side only at the moment)
    • REST (via rubbs-reststorage - browser-only, at the moment)

Examples

function User(data) {
  this.setData(data);
}

User.prototype.clazz = 'user';

Tubbs(User, {
  dataStore: new Memory(User),
  basicProperties: ['username', 'first', 'last'],
  validation: [
    Validate.required("username"),
    Validate.lengthOf("username", { min: 5 })
  ],
});

Tubbs(User, {
  dataStore: new RaceCar(User),
  basicProperties: ['username', 'first', 'last'],
  validation: [
    Validate.required("username"),
    Validate.lengthOf("username", { min: 5 })
  ],
});


Object.defineProperty(User, 'name', {
  get: function() {
    return ((this.first || '') + ' ' + (this.last || '')).trim();
  },
  enumerable: true
});

Create a new user

var user = new User({
  username: "kbacon",
  first: "Kevin",
  last: "Bacon"
});

Get one of its virtual properties

console.log(user.name);
// -> 'Kevin Bacon'

Observe property value changes

User.on('change', function(instance, property, old, value) {
  // When any property changes on any User instance
});

User.on('change:name', function(instance, old, value) {
  // When the "name" property changes on any User instance
});

var user = new User();

user.on('change', function(property, old, value) {
  // When any property changes on a specific User instance
});

user.on('change:name', function(old, value) {
  // When the "name" property changes on a specific User instance
});

Observe model creation and deletion and save

User.on('save', function(instance) {
  // When any User model is saved.
});

User.on('delete', function(instance) {
  // When any User model is deleted.
});

var user = new User();

user.on('save', function(instance) {
  // When a specific User model is saved.
});

user.on('delete', function(instance) {
  // When a specific User model is deleted.
});

Serialize the instance to pure JSON

console.log(JSON.stringify(user));
// ->
// {
//   "username": "kbacon",
//   "first": "Kevin",
//   "last": "Bacon"
// }

Roadmap (Also, see TODO.md)

  • Validation
    • allowBlank option?
    • shortcut format validator strings, such as "email" and "phone"
  • Add a beforeSave option: beforeSave: function() { this.dateModified = new Date(); }
  • Figure out how to notify others when an error is thrown.
  • More unit test coverage
  • Schema-based generated class methods: Person.findAllByAge(50, cb)
  • Documentation Pages
    • Model instance API
    • Model class API
    • Validators
  • CouchStore
  • MongoStore
  • RedisStore
  • LocalStorageStore

tubbs's People

Contributors

kristianmandrup avatar

Watchers

 avatar James Cloos avatar  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.