Skip to content

Commit

Permalink
Merge pull request #37 from kurtmilam/master
Browse files Browse the repository at this point in the history
Fixes #36 - Add support for CouchDB lists
  • Loading branch information
janmonschke committed Apr 2, 2012
2 parents de45b6b + 8c885e3 commit a8e8afd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 8 additions & 1 deletion backbone-couchdb.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions backbone-couchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a8e8afd

Please sign in to comment.