-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackbone-mongodb.js
59 lines (45 loc) · 1.43 KB
/
backbone-mongodb.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
// backbone-mongodb.js
// (c) 2011 Done.
(function() {
// Save a reference to the global object.
var root = this;
// Require Backbone and Underscore if we're on the server, and it's not already present
var isServer = (typeof require !== 'undefined');
var Backbone = root.Backbone;
var _ = root._;
var MongoDBDocument;
if (!Backbone && isServer) Backbone = require('backbone');
if (!_ && isServer) _ = require('underscore');
var MongoDb = {};
// MongoDb models
// --------------
MongoDb.models = {
Document: Backbone.Model.extend({
idAttribute: '_id', // provides the mongo _id for documents
models: {}, // mapping of attributes to models (optional)
get : function(attr) {
return this._prepareAttribute(attr);
},
// Create the attribute with the right
_prepareAttribute : function(attr) {
var value = this.attributes[attr];
if(attr in this.models) {
value = new this.models[attr](value);
value.container = this;
}
return value;
}
}),
};
// Patch Backbone
// --------------
// Add mongoDB behavior to the Document
if (isServer) {
/*
var MongoDBDocument = require('./lib/mongodb-document');
_.extend(MongoDb.models.Document.prototype, MongoDBDocument);
*/
}
Backbone.MongoDb = MongoDb;
_.extend(Backbone, MongoDb.models);
}).call(this);