Remote Objects API

RemoteObjects(options)

Create a new RemoteObjects with the given options.

var remoteObjects = require('strong-remoting').create();
Arguments
Name Type Description
options Object

remoteObjects.connect(url, name)

Create a connection to a remoting server.

Arguments
Name Type Description
url String

Server root

name String

Name of the adapter (eg. "rest")

remoteObjects.invoke(method, [ctorArgs], [args], [callback])

Invoke a method on a remote server using the connected adapter.

Arguments
Name Type Description
method String

The remote method string

[ctorArgs] String

Constructor arguments (for prototype methods)

[args] String

Method arguments

[callback] Function

callback

Callback
Name Type Description
err Error
arg... Any

remoteObjects.adapter(name)

Get an adapter by name.

Arguments
Name Type Description
name String

The adapter name

remoteObjects.classes()

Get all classes.

remoteObjects.addClass(sharedClass)

Add a shared class.

Arguments
Name Type Description
sharedClass SharedClass

remoteObjects.findMethod(methodString)

Find a method by its string name.

Example Method Strings:

  • MyClass.prototype.myMethod
  • MyClass.staticMethod
  • obj.method
Arguments
Name Type Description
methodString String

remoteObjects.methods()

List all methods.

remoteObjects.toJSON()

Get as JSON.

remoteObjects.before(methodMatch, hook)

Execute the given function before the matched method string.

Examples:

// Do something before our `user.greet` example, earlier.
remotes.before('user.greet', function (ctx, next) {
  if((ctx.req.param('password') || '').toString() !== '1234') {
    next(new Error('Bad password!'));
  } else {
    next();
  }
});

// Do something before any `user` method.
remotes.before('user.*', function (ctx, next) {
  console.log('Calling a user method.');
  next();
});

// Do something before a `dog` instance method.
remotes.before('dog.prototype.*', function (ctx, next) {
  var dog = this;
  console.log('Calling a method on "%s".', dog.name);
  next();
});
Arguments
Name Type Description
methodMatch String

The glob to match a method string

hook Function
Callback
Name Type Description
ctx Context

The adapter specific context

next Function

Call with an optional error object

method SharedMethod

The SharedMethod object

remoteObjects.after(methodMatch, hook)

Execute the given hook function after the matched method string.

Examples:

// Do something after the `speak` instance method.
// NOTE: you cannot cancel a method after it has been called.
remotes.after('dog.prototype.speak', function (ctx, next) {
  console.log('After speak!');
  next();
});

// Do something before all methods.
remotes.before('**', function (ctx, next, method) {
  console.log('Calling:', method.name);
  next();
});

// Modify all returned values named `result`.
remotes.after('**', function (ctx, next) {
  ctx.result += '!!!';
  next();
});
Arguments
Name Type Description
methodMatch String

The glob to match a method string

hook Function
Callback
Name Type Description
ctx Context

The adapter specific context

next Function

Call with an optional error object

method SharedMethod

The SharedMethod object

remoteObjects.invokeMethodInContext(ctx, method, cb)

Invoke the given shared method using the supplied context. Execute registered before/after hooks.

Arguments
Name Type Description
ctx
method
cb

RemoteObjects.defineType(name, converter)

Define a named type conversion. The conversion is used when a SharedMethod argument defines a type with the given name.

remotes.defineType('MyType', function(val, ctx) {
  // use the val and ctx objects to return the concrete value
  return new MyType(val); 
});

Note: the alias remotes.convert() is deprecated.

Arguments
Name Type Description
name String

The type name

converter Function