Skip to content

Instantly share code, notes, and snippets.

@gcpantazis
Created April 4, 2012 19:49
Show Gist options
  • Save gcpantazis/2305089 to your computer and use it in GitHub Desktop.
Save gcpantazis/2305089 to your computer and use it in GitHub Desktop.
Custom class types for Backbone.js
var CustomClass = Backbone.CustomClass = function(options) {
this.cid = _.uniqueId('CustomClass');
this._configure(options || {});
this.initialize.apply(this, arguments);
};
var classOptions = ['attributes'];
_.extend(CustomClass.prototype, null, {
initialize: function(){},
_configure: function(options) {
if (this.options) options = _.extend({}, this.options, options);
for (var i = 0, l = classOptions.length; i < l; i++) {
var attr = classOptions[i];
if (options[attr]) this[attr] = options[attr];
}
this.options = options;
}
});
var extend = function (protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
return child;
};
CustomClass.extend = extend;
var ctor = function(){};
var inherits = function(parent, protoProps, staticProps) {
var child;
if (protoProps && protoProps.hasOwnProperty('constructor')) {
child = protoProps.constructor;
} else {
child = function(){ parent.apply(this, arguments); };
}
_.extend(child, parent);
ctor.prototype = parent.prototype;
child.prototype = new ctor();
if (protoProps) _.extend(child.prototype, protoProps);
if (staticProps) _.extend(child, staticProps);
child.prototype.constructor = child;
child.__super__ = parent.prototype;
return child;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment