Skip to content

Latest commit

 

History

History
81 lines (58 loc) · 1.84 KB

instance-class-methods.md

File metadata and controls

81 lines (58 loc) · 1.84 KB

Instance and Class Methods in Offshore Models

Instance methods

You can attach instance methods to a model which will be available on any record returned from a query. These are defined as functions in your model attributes.

var User = Offshore.Collection.extend({

  identity: 'user',
  connection: 'local-postgresql',

  attributes: {
    firstName: 'string',
    lastName: 'string',
    fullName: function() {
      return this.firstName + ' ' + this.lastName;
    }
  }
});

toObject/toJSON

The toObject() method will return the currently set model values only, without any of the instance methods attached. Useful if you want to change or remove values before sending to the client.

However we provide an even easier way to filter values before returning to the client by allowing you to override the toJSON() method in your model.

Example of filtering a password in your model definition:

var User = Offshore.Collection.extend({

  identity: 'user',
  connection: 'local-postgresql',

  attributes: {
    name: 'string',
    password: 'string',

    // Override toJSON instance method to remove password value
    toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      return obj;
    }
  }
});

Class Methods

"Class" methods are functions available at the top level of a model. They can be called anytime after a Offshore instance has been initialized.

These are useful if you would like to keep model logic in the model and have reusable functions available.

var Foo = Offshore.Collection.extend({

  identity: 'foo',
  connection: 'my-local-postgresql',

  attributes: {},

  // A "class" method
  method1: function() {}

});

// Example
Foo.method1()