bbnano
Advanced tools
Comparing version 0.1.1 to 0.2.0
24
index.js
@@ -0,11 +1,4 @@ | ||
var _ = require('underscore'); | ||
var Backbone = require('backbone'); | ||
// Helper that gets the database from a model. | ||
var getDatabase = function(obj) { | ||
if (typeof(obj.database) === 'function') | ||
return obj.database(); | ||
else | ||
return obj.database; | ||
}; | ||
// A model subclass that uses our sync. | ||
@@ -17,3 +10,3 @@ var Model = Backbone.Model.extend({ | ||
if (this.collection) { | ||
return getDatabase(this.collection); | ||
return _.result(this.collection, 'database'); | ||
} | ||
@@ -27,3 +20,3 @@ }, | ||
sync: function(method, model, options) { | ||
var db = getDatabase(model); | ||
var db = _.result(model, 'database'); | ||
@@ -64,8 +57,13 @@ var success = options.success || function() {}; | ||
read: function(db, callback) { | ||
db.list(callback); | ||
db.list({ include_docs: true }, callback); | ||
}, | ||
// Provide a default `parse` override that extracts documents. | ||
parse: function(res) { | ||
return _.pluck(res.rows, 'doc'); | ||
}, | ||
// Override sync with ours. | ||
sync: function(method, model, options) { | ||
var db = getDatabase(model); | ||
var db = _.result(model, 'database'); | ||
@@ -78,3 +76,3 @@ var success = options.success || function() {}; | ||
model.read(db, function(err, res) { | ||
err ? error(err) : success(res.rows); | ||
err ? error(err) : success(res); | ||
}); | ||
@@ -81,0 +79,0 @@ break; |
@@ -5,3 +5,3 @@ { | ||
"author": "Stéphan Kochen <stephan@kochen.nl>", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/Two-Screen/bbnano", | ||
@@ -13,2 +13,3 @@ "repository": { | ||
"dependencies": { | ||
"underscore": "1", | ||
"backbone": "0.9" | ||
@@ -15,0 +16,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
## bbnano | ||
## bbnano [![Build Status](https://secure.travis-ci.org/Two-Screen/bbnano.png)](http://travis-ci.org/Two-Screen/bbnano) | ||
@@ -3,0 +3,0 @@ Extension for [Backbone.js] to sync with [CouchDB] using [nano] on [Node.js]. |
@@ -25,3 +25,3 @@ var assert = require('assert'); | ||
test('basic usage', function(t) { | ||
t.plan(14); | ||
t.plan(15); | ||
@@ -57,2 +57,3 @@ var collection = new bbnano.Collection(); | ||
t.is(collection.length, 1, 'collection must have one model'); | ||
t.is(collection.at(0).get('test'), 'foo', 'attribute must match'); | ||
update(); | ||
@@ -59,0 +60,0 @@ })); |
6801
2
156
+ Addedunderscore@1