-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.js
103 lines (85 loc) · 4.26 KB
/
Utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
define(function (require) {
var config = require('config')
, _ = require('underscore')
// Shared empty constructor function to aid in prototype-chain creation.
var ctor = function () {}
var Utils = {
// Common scoped error logging
scopedError:function (name) {
return function (e) {
console.error.call(console, 'Hackbone' + (name ? '/' + name : '') + ": " + e, Array.prototype.slice.call(arguments, 1));
throw name + ": " + e
}
},
// Commong scoped logging
scopedLog:function (name) {
return function (m) {
console.log.call(console, "[Hackbone" + (name ? '/' + name : '') + "] " + m, Array.prototype.slice.call(arguments, 1));
}
},
namedExtend:function (name) {
return function (protoProps, classProps) {
return Utils.extend.call(this, name, protoProps, classProps)
}
},
// The self-propagating extend function that Backbone classes use.
extend:function (name, protoProps, classProps) {
if (typeof name == 'object') {
classProps = protoProps;
protoProps = name;
name = null;
}
// Little hack to name the objects when debugging
if (name && config.inheritNames) {
var self = this
, f = protoProps.hasOwnProperty('constructor') ? protoProps.constructor : self
, evalStr = ''
var escapedName = name.replace(/[-\/\(\)]+/g, '_');
evalStr += 'var ' + escapedName + ' = function(){ f.apply(this, arguments); };';
evalStr += 'protoProps.constructor = ' + escapedName + ';'
eval(evalStr);
}
var child = Utils.inherits(this, protoProps, classProps);
child.extend = this.extend || Utils.extend;
return child;
},
// Helper function to get a value from a Backbone object as a property
// or as a function.
getValue:function (object, prop) {
if (!(object && object[prop])) return null;
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
},
// Helper function to correctly set up the prototype chain, for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
inherits:function (parent, protoProps, staticProps) {
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && protoProps.hasOwnProperty('constructor')) {
child = protoProps.constructor;
} else {
child = function () { parent.apply(this, arguments); };
}
// Inherit class (static) properties from parent.
_.extend(child, parent);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
ctor.prototype = parent.prototype;
child.prototype = new ctor();
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);
// Add static properties to the constructor function, if supplied.
if (staticProps) _.extend(child, staticProps);
// Correctly set child's `prototype.constructor`.
child.prototype.constructor = child;
// Set a convenience property in case the parent's prototype is needed later.
child.__super__ = parent.prototype;
return child;
}
}
return Utils;
}
);