Skip to content

Commit

Permalink
use CoffeeScript-compliant Backbone.Model.extend
Browse files Browse the repository at this point in the history
  • Loading branch information
James McKinney committed Apr 14, 2013
1 parent 6777110 commit a625966
Showing 1 changed file with 65 additions and 44 deletions.
109 changes: 65 additions & 44 deletions core/Core.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,66 +7,87 @@ AjaxSolr = function () {};


/** /**
* @namespace Baseclass for all classes * @namespace Baseclass for all classes
* @see https://github.com/documentcloud/backbone/blob/51eed189bf4d25877be4acdf51e0a4c6039583c5/backbone.js#L243
*/ */
AjaxSolr.Class = function () {}; AjaxSolr.Class = function(attributes) {
AjaxSolr.extend(this, attributes);
};


/** /**
* A class 'extends' itself into a subclass. * A class 'extends' itself into a subclass.
* *
* @static * @static
* @param properties The properties of the subclass. * @param protoProps The properties of the subclass.
* @returns A function that represents the subclass. * @returns A function that represents the subclass.
* @see https://github.com/documentcloud/backbone/blob/51eed189bf4d25877be4acdf51e0a4c6039583c5/backbone.js#L1516
*/ */
AjaxSolr.Class.extend = function (properties) { AjaxSolr.Class.extend = function (protoProps, staticProps) {
var klass = this; // Safari dislikes 'class' var parent = this;
// The subclass is just a function that when called, instantiates itself. var child;
// Nothing is _actually_ shared between _instances_ of the same class.
var subClass = function (options) { // The constructor function for the new subclass is either defined by you
// 'this' refers to the subclass, which starts life as an empty object. // (the "constructor" property in your `extend` definition), or defaulted
// Add its parent's properties, its own properties, and any passed options. // by us to simply call the parent's constructor.
AjaxSolr.extend(this, new klass(options), properties, options); if (protoProps && Object.prototype.hasOwnProperty.call(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
} }
// Allow the subclass to extend itself into further subclasses.
subClass.extend = this.extend; // Add static properties to the constructor function, if supplied.
return subClass; AjaxSolr.extend(child, parent, staticProps);

// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;

// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) AjaxSolr.extend(child.prototype, protoProps);

// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;

return child;
}; };


/** /**
* A simplified version of jQuery's extend function.
*
* @static * @static
* @see http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js * @see https://github.com/documentcloud/underscore/blob/7342e289aa9d91c5aacfb3662ea56e7a6d081200/underscore.js#L789
*/ */
AjaxSolr.extend = function () { AjaxSolr.extend = function (child) {
var target = arguments[0] || {}, i = 1, length = arguments.length, options; // From _.extend
for (; i < length; i++) { var obj = Array.prototype.slice.call(arguments, 1);
if ((options = arguments[i]) != null) {
for (var name in options) { // From _.extend
var src = target[name], copy = options[name]; var iterator = function(source) {
if (target === copy) { if (source) {
continue; for (var prop in source) {
} child[prop] = source[prop];
if (copy && typeof copy == 'object' && !copy.nodeType) { }
target[name] = AjaxSolr.extend(src || (copy.length != null ? [] : {}), copy); }
} };
else if (copy && src && typeof copy == 'function' && typeof src == 'function') {
target[name] = (function(superfn, fn) { // From _.each
return function () { if (obj == null) return;
var tmp = this._super, ret; if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
this._super = superfn; obj.forEach(iterator);
ret = fn.apply(this, arguments); } else if (obj.length === +obj.length) {
this._super = tmp; for (var i = 0, l = obj.length; i < l; i++) {
return ret; iterator.call(undefined, obj[i], i, obj);
}; }
})(src, copy); } else {
} for (var key in obj) {
else if (copy !== undefined) { if (Object.prototype.hasOwnProperty.call(obj, key)) {
target[name] = copy; iterator.call(undefined, obj[key], key, obj);
}
} }
} }
} }
return target;
return child;
}; };


/** /**
Expand Down

0 comments on commit a625966

Please sign in to comment.