Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow collections to specify their own design document #38

Merged
merged 2 commits into from

2 participants

Nick Pappas Jan Monschke
Nick Pappas

Much like we can specify a view other than "byCollection", this PRQ allows a collection to specify its own design document if it differs from the default.

Jan Monschke
Owner

I just accepted another PRQ in the main repo, can you fetch the changes and merge them, so I can merge this one to the main repo as well?

Nick Pappas

Done-- It should merge now, let me know if you need anything else.

Jan Monschke
Owner

Awesome thx :)
If you like you can write a little text about that feature so I can put in in the documentation under 'recipes' at http://janmonschke.com/projects/backbone-couchdb.html

Jan Monschke janmonschke merged commit bd0a2ea into from
Nick Pappas

Heh, it's pretty straightforward:

If you need your collection to be sourced from a different design document, you can use the db.ddoc Collection option to specify a new design document:

  Backbone.couch_connector.config.ddoc_name = "my_main_design_doc"

  var mainCollection = Backbone.Collection.extend({
    db : {
      view : "myCollection"
    }
    ....
  });

  var otherCollection = Backbone.Collection.extend({
    db : {
      view : "myCollection",
      ddoc : "my_other_design_doc"
    }
    ....
  });

This results in mainCollection sourcing from _design/my_main_design_doc/_view/myCollection and otherCollection sourcing from _design/my_other_design_doc/_view/myCollection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 5 deletions.
  1. +5 −2 backbone-couchdb.coffee
  2. +5 −3 backbone-couchdb.js
7 backbone-couchdb.coffee
View
@@ -53,12 +53,15 @@ 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
+ _ddoc = @config.ddoc_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.ddoc?
+ _ddoc = coll.db.ddoc
if coll.db.keys?
keys = coll.db.keys
if coll.db.list?
@@ -103,9 +106,9 @@ Backbone.couch_connector = con =
delete _opts.keys
if _list
- @helpers.make_db().list "#{@config.ddoc_name}/#{_list}", "#{_view}", _opts
+ @helpers.make_db().list "#{_ddoc}/#{_list}", "#{_view}", _opts
else
- @helpers.make_db().view "#{@config.ddoc_name}/#{_view}", _opts
+ @helpers.make_db().view "#{_ddoc}/#{_view}", _opts
# Reads a model from the couchdb by it's ID
8 backbone-couchdb.js
View
@@ -54,9 +54,10 @@ backbone-couchdb.js is licensed under the MIT license.
}
},
read_collection: function(coll, opts) {
- var keys, _opts, _view, _list,
+ var keys, _opts, _view, _ddoc, _list
_this = this;
_view = this.config.view_name;
+ _ddoc = this.config.ddoc_name;
_list = this.config.list_name;
keys = [this.helpers.extract_collection_name(coll)];
if (coll.db != null) {
@@ -64,6 +65,7 @@ backbone-couchdb.js is licensed under the MIT license.
coll.listen_to_changes();
}
if (coll.db.view != null) _view = coll.db.view;
+ if (coll.db.ddoc != null) _ddoc = coll.db.ddoc;
if (coll.db.keys != null) keys = coll.db.keys;
if (coll.db.list != null) _list = coll.db.list;
}
@@ -96,9 +98,9 @@ backbone-couchdb.js is licensed under the MIT license.
delete _opts.keys;
}
if (_list != null) {
- return this.helpers.make_db().list("" + this.config.ddoc_name + "/" + _list, "" + _view, _opts);
+ return this.helpers.make_db().list("" + _ddoc + "/" + _list, "" + _view, _opts);
} else {
- return this.helpers.make_db().view("" + this.config.ddoc_name + "/" + _view, _opts);
+ return this.helpers.make_db().view("" + _ddoc + "/" + _view, _opts);
}
},
read_model: function(model, opts) {
Something went wrong with that request. Please try again.