Skip to content
matthewrobbins edited this page Jun 12, 2013 · 7 revisions

All sudo.js library objects inherit from the Base class, giving them a unique id (this.uid), the ability to utilize delegates, plus the construct and base convenience methods. While the uid, a simple integer, needs no further explanation, the others mentioned warrant a deeper dive.

###construct([el,] [data])

From a constructor in a subclass, you will need to call the parent's constructor at some point. As most of your applications Javascript class objects will be a subclass of a sudo.js class, the convenience method, construct, is available to use in your own object's constructor to mitigate the need to find the current object's prototype's constructor manually. Illustrated in code using a sudo.View subclass as an example:

var Foo = function(el, data) {
  // without the construct method you would have to:
  object.getPrototypeOf(this).constructor.apply(this, arguments);
  // instead you can:
  this.construct(el, data);
};
Foo.prototype = Object.create(sudo.View.prototype);

The parameter signature for construct should match the expected signature of the constructor being called (usually the same as the constructor function containing it).

This is the typical use-case in everyday development with the sudo.js library. Inheritance in Javascript in its current form (ES5) is rather nuanced however so there is an entire wiki page devoted to it.

###base(methodName[, *args])

This is another variation of a super type functionality, similar to construct, but for locating and calling methods on the current object's prototype chain after you have overridden them. Let's say you have subclassed the sudo.Model class and want override the set method so you can log the key to the console:

var Foo = function(data) {
  this.construct(data);
};

Foo.prototype = Object.create(sudo.Model.prototype);

Foo.prototype.set = function set(key, value) {
  console.log(key);
  // call the 'super' function
  this.base('set', key, value);
};

###Delegates

A delegate is a specialized class object designed to do a very specific task for a delegator. Any sudo class object can utilize delegates (be a delegator), and any sudo class type (or a "plain old Javascript" object for that matter) can be a delegate. Delegates, like extensions, exist to prevent unnecessary subclassing, to extend the usefulness of already existing objects. The primary advantage that delegates have over extensions is that they have their own unique constructors (extension objects do not).

####addDelegate(classObject)

Push an instance of a class object into this object's delegates collection (an array at this.delegates). Also established the relationship between delegator and delegate.

####delegate(role[, methodName])

The primary interaction with a delegate is through this method, as it can perform two tasks:

  1. Passed only the role argument, return the delegate object itself. This is synonymous to getDelegate.
  2. Passed both the role and a method name existing on the delegate, return a reference to the delegate's function bound to the delegates scope

Number one fetches the delegate, which can then be used. The built-in Change Delegate for example is a sublass of sudo.Model, so a delegator could fetch it's Change Delegate and call get on it by:

var someFoo = this.delegate('change').get('foo');

This isn't that common of a use case as delegates, as you will see, are meant to function automagically once instantiated.

Number two returns a reference to a delegate's method: Let's say you have a class that expects to receive a large object literal, as its the "success" callback to an "$.ajax" call, and has to inspect it for certain key:value pairs and do stuff with said values. Many times this sort of common situation is riddled with ugly code like this:

ajaxSuccessFunction(data) {
  if(data && data.foo) {
    this.doWithFoo(data.foo);
  }
  if(data && data.bar && data.bar.baz) {
    this.doWithBaz(data.bar.baz);
  }
  // continue ad nauseum
};

A class could instead utilize the built-in Data Delegate filter function as a target for the "success" callback and never worry about it. You would achieve in some fashion like so:

// in some function where you are using jQuery XHR
$.ajax({
  ...
  dataType: 'json',
  success: this.delegate('data', 'filter),
  ...
});

This would transparently filter through the returned object literal and call methods on the delegator that you had indicated, see the Data Delegate page, with the values located at only the keys you were interested in (if they indeed exist).

####removeDelegate(role)

Remove the class object in this object's delagates collection with a "role" matchin the passed in argument. Also, remove the delegator reference from the delgate.

####getDelegate(role)

Fetch the delegate in this objects delegates collection with a "role" matching the passed in argument.

Clone this wiki locally