Created
April 4, 2012 19:49
-
-
Save gcpantazis/2305089 to your computer and use it in GitHub Desktop.
Custom class types for Backbone.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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