Fixes #36 - Add support for CouchDB lists #37

Merged
merged 2 commits into from Apr 2, 2012
View
@@ -10,6 +10,7 @@ Backbone.couch_connector = con =
db_name : "backbone_connect"
ddoc_name : "backbone_example"
view_name : "byCollection"
+ list_name : null
# if true, all Collections will have the _changes feed enabled
global_changes : false
# change the databse base_url to be able to fetch from a remote couchdb
@@ -52,13 +53,16 @@ Backbone.couch_connector = con =
# Reads all docs of a collection based on the byCollection view or a custom view specified by the collection
read_collection : (coll, opts) ->
_view = @config.view_name
+ _list = @config.list_name
keys = [@helpers.extract_collection_name coll]
if coll.db?
coll.listen_to_changes() if coll.db.changes or @config.global_changes
if coll.db.view?
_view = coll.db.view
if coll.db.keys?
keys = coll.db.keys
+ if coll.db.list?
+ _list = coll.db.list
_opts =
keys : keys
@@ -98,7 +102,10 @@ Backbone.couch_connector = con =
if coll.db? and coll.db.view? and not coll.db.keys?
delete _opts.keys
- @helpers.make_db().view "#{@config.ddoc_name}/#{_view}", _opts
+ if _list
+ @helpers.make_db().list "#{@config.ddoc_name}/#{_list}", "#{_view}", _opts
+ else
+ @helpers.make_db().view "#{@config.ddoc_name}/#{_view}", _opts
# Reads a model from the couchdb by it's ID
View
@@ -15,6 +15,7 @@ backbone-couchdb.js is licensed under the MIT license.
db_name: "backbone_connect",
ddoc_name: "backbone_example",
view_name: "byCollection",
+ list_name: null,
global_changes: false,
base_url: null
},
@@ -53,16 +54,18 @@ backbone-couchdb.js is licensed under the MIT license.
}
},
read_collection: function(coll, opts) {
- var keys, _opts, _view,
+ var keys, _opts, _view, _list,
_this = this;
_view = this.config.view_name;
+ _list = this.config.list_name;
keys = [this.helpers.extract_collection_name(coll)];
if (coll.db != null) {
if (coll.db.changes || this.config.global_changes) {
coll.listen_to_changes();
}
if (coll.db.view != null) _view = coll.db.view;
if (coll.db.keys != null) keys = coll.db.keys;
+ if (coll.db.list != null) _list = coll.db.list;
}
_opts = {
keys: keys,
@@ -92,7 +95,11 @@ backbone-couchdb.js is licensed under the MIT license.
if ((coll.db != null) && (coll.db.view != null) && !(coll.db.keys != null)) {
delete _opts.keys;
}
- return this.helpers.make_db().view("" + this.config.ddoc_name + "/" + _view, _opts);
+ if (_list != null) {
+ return this.helpers.make_db().list("" + this.config.ddoc_name + "/" + _list, "" + _view, _opts);
+ } else {
+ return this.helpers.make_db().view("" + this.config.ddoc_name + "/" + _view, _opts);
+ }
},
read_model: function(model, opts) {
if (!model.id) {