-
Notifications
You must be signed in to change notification settings - Fork 8
/
base.js
180 lines (135 loc) · 4.32 KB
/
base.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/** @module base */
(function (factory) {
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined' && typeof require !== 'undefined') {
// CommonJS
module.exports = factory(require('underscore'));
} else {
// running in browser
window.warehouse = window.warehouse || {};
window.warehouse.BaseBackend = factory(_);
}
})(function(_) {
/** @class BaseBackend */
function BaseBackend() {
this.initialize.apply(this, arguments);
}
_.extend(BaseBackend.prototype,
/** @lends BaseBackend# */
{
/** Initialize the backend instance */
initialize: function(options) {
options = _.extend({}, options || {});
this.options = options;
},
/** List storage names (e.g. tables for SQL, collections for MongoDB)
@abstract */
objectStoreNames: function() {
},
/** Get object store
* @abstract */
objectStore: function(name, options) {
},
/** Create object store
@abstract */
createObjectStore: function(name, options) {
},
/** Delete object store
@abstract */
deleteObjectStore: function(name) {
}
});
/** @class BaseStore */
function BaseStore() {
this.initialize.apply(this, arguments);
}
_.extend(BaseStore.prototype,
/** @lends BaseStore# */
{
/** Initialize the backend instance
@constructs */
initialize: function(backend, name, options) {
options = _.extend({keyPath: 'id'}, options || {});
this.options = options;
this.backend = backend;
this.name = name;
this.keyPath = this.options.keyPath;
},
/** Get value (aka READ)
@abstract */
get: function(directives) {
},
/** Add value (aka CREATE, insert)
@abstract */
add: function(object, directives) {
},
/** Get value (aka UPDATE)
@abstract */
put: function(object, directives) {
},
/** Delete value (aka DELETE)
@abstract */
'delete': function(directives) {
},
/** Execute RQL query
@abstract */
query: function(query) {
},
/** Delete all items
@abstract */
clear: function() {
},
/** Extract key value from object
@private */
_getObjectKey: function(obj, key) {
if (typeof key === 'object') {
key = key.key;
}
return key || obj[this.keyPath];
}
});
// Taken from Backbone:
// Shared empty constructor function to aid in prototype-chain creation.
var ctor = function(){};
// 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.
var 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;
};
// The self-propagating extend function that Backbone classes use.
var extend = function (protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
return child;
};
/** Class inheritance */
BaseStore.extend = extend;
/** Class inheritance */
BaseBackend.extend = extend;
BaseBackend.BaseStore = BaseStore;
return BaseBackend;
});