Skip to content
This repository

Allow collections to specify their own design document #38

Merged
merged 2 commits into from about 2 years ago

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 April 02, 2012
Jan Monschke janmonschke closed this April 02, 2012
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.
7  backbone-couchdb.coffee
@@ -53,12 +53,15 @@ Backbone.couch_connector = con =
53 53
   # Reads all docs of a collection based on the byCollection view or a custom view specified by the collection
54 54
   read_collection : (coll, opts) ->
55 55
     _view = @config.view_name
  56
+    _ddoc = @config.ddoc_name
56 57
     _list = @config.list_name
57 58
     keys = [@helpers.extract_collection_name coll]
58 59
     if coll.db?
59 60
       coll.listen_to_changes() if coll.db.changes or @config.global_changes
60 61
       if coll.db.view?
61 62
         _view = coll.db.view
  63
+      if coll.db.ddoc?
  64
+        _ddoc = coll.db.ddoc
62 65
       if coll.db.keys?
63 66
         keys = coll.db.keys 
64 67
       if coll.db.list?
@@ -103,9 +106,9 @@ Backbone.couch_connector = con =
103 106
       delete _opts.keys
104 107
     
105 108
     if _list 
106  
-      @helpers.make_db().list "#{@config.ddoc_name}/#{_list}", "#{_view}", _opts   
  109
+      @helpers.make_db().list "#{_ddoc}/#{_list}", "#{_view}", _opts   
107 110
     else
108  
-      @helpers.make_db().view "#{@config.ddoc_name}/#{_view}", _opts    
  111
+      @helpers.make_db().view "#{_ddoc}/#{_view}", _opts    
109 112
 
110 113
 
111 114
   # Reads a model from the couchdb by it's ID 
8  backbone-couchdb.js
@@ -54,9 +54,10 @@ backbone-couchdb.js is licensed under the MIT license.
54 54
       }
55 55
     },
56 56
     read_collection: function(coll, opts) {
57  
-      var keys, _opts, _view, _list,
  57
+      var keys, _opts, _view, _ddoc, _list
58 58
         _this = this;
59 59
       _view = this.config.view_name;
  60
+      _ddoc = this.config.ddoc_name;
60 61
       _list = this.config.list_name;
61 62
       keys = [this.helpers.extract_collection_name(coll)];
62 63
       if (coll.db != null) {
@@ -64,6 +65,7 @@ backbone-couchdb.js is licensed under the MIT license.
64 65
           coll.listen_to_changes();
65 66
         }
66 67
         if (coll.db.view != null) _view = coll.db.view;
  68
+        if (coll.db.ddoc != null) _ddoc = coll.db.ddoc;
67 69
         if (coll.db.keys != null) keys = coll.db.keys;
68 70
         if (coll.db.list != null) _list = coll.db.list;
69 71
       }
@@ -96,9 +98,9 @@ backbone-couchdb.js is licensed under the MIT license.
96 98
         delete _opts.keys;
97 99
       }
98 100
       if (_list != null) {
99  
-        return this.helpers.make_db().list("" + this.config.ddoc_name + "/" + _list, "" + _view, _opts);
  101
+        return this.helpers.make_db().list("" + _ddoc + "/" + _list, "" + _view, _opts);
100 102
       } else {
101  
-        return this.helpers.make_db().view("" + this.config.ddoc_name + "/" + _view, _opts);
  103
+        return this.helpers.make_db().view("" + _ddoc + "/" + _view, _opts);
102 104
       }
103 105
     },
104 106
     read_model: function(model, opts) {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.