This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

js-model Cheatsheet (Archive)

A historical reference guide for js-model: client-side models, attributes, validations, persistence adapters, and event bindings.

· Published

js-model is a minimal JavaScript model library for client-side applications.

Model Definition & Attributes

const User = Model('user', function () {
  this.attr('name', String);
  this.attr('email', String);
  this.attr('admin', Boolean);

  this.validate(function () {
    if (!this.attr('email')) {
      this.errors.add('email', 'is required');
    }
  });
});

const user = new User({ name: 'Alice', email: '[email protected]' });
user.attr('name');               // "Alice"
user.attr('name', 'Bob');        // updates name
user.isValid();                  // true

Collections & Persistence

User.add(user);                  // Add instance to collection
User.all();                      // Return array of all users
User.find(1);                    // Find by primary key ID
User.select(u => u.attr('admin'));// Filter by condition

References

Related topics