Skip to content

Commit

Permalink
Collection spec and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Covell committed Aug 31, 2011
1 parent 309beb4 commit 3dcc493
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
29 changes: 2 additions & 27 deletions backbone-mongodb.js
Expand Up @@ -41,31 +41,7 @@
return value;
}
}),
},

// MongoDb collections
// -------------------
MongoDb.collections = {

DocumentCollection: Backbone.Collection.extend({
model: MongoDb.models.Document,

fetch: function(options) {
this.__super__.fetch(options);
},

create: function(model, options) {
this.__super__.create(model, options);
},

remove: function(models, options) {
this.__super__.remove(models, options);
},

}),

};

},

// Patch Backbone
// --------------
Expand All @@ -78,6 +54,5 @@

Backbone.MongoDb = MongoDb;
_.extend(Backbone, MongoDb.models);
_.extend(Backbone, MongoDb.collections);


}).call(this);
32 changes: 29 additions & 3 deletions spec/collection_spec.js
Expand Up @@ -5,7 +5,19 @@ var assert = require('assert'),
BackboneMongoDb = require('../backbone-mongodb'),
Backbone = require('backbone');

vows.describe('Validation').addBatch({
var Monkey = Backbone.Document.extend({
});

var MonkeyCollection = Backbone.Collection.extend({
model : Monkey,
});

var Document = Backbone.Document.extend({
collectionName: 'document',
models : { 'monkeys': MonkeyCollection },
});

vows.describe('Collection').addBatch({

// Set up the database
// -------------------
Expand All @@ -18,9 +30,23 @@ vows.describe('Validation').addBatch({
}
}

// Validate XXX
// Validate object type assignment
// -------------------------------

}).addBatch({
'a new document with monkeys': {
topic: function() {
var document = new Document({ monkeys: [ { name: 'Monkey 1' }, { name : 'Monkey 2' } ] });
document.save(null, this.callback);
},
'should have a monkey collection': function(err, document) {
assert.isTrue(document.get('monkeys') instanceof MonkeyCollection);
},
'monkey collection should have monkeys': function(err, document) {
assert.isTrue(document.get('monkeys').at(0) instanceof Monkey);
},
'monkey in collection should be the right monkey': function(err, document) {
assert.equal(document.get('monkeys').at(0).get('name'), 'Monkey 1');
}
}

}).export(module);

0 comments on commit 3dcc493

Please sign in to comment.