• Stars
    star
    105
  • Rank 321,504 (Top 7 %)
  • Language
    JavaScript
  • Created over 13 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Getting started

First, you'll need to include some new script tags on your pages:

<script src="underscore.js"></script>
<script src="jquery.js"></script>
<script src="jquery.couch.js"></script>
<script src="backbone.js"></script>
<script src="backbone.couchdb.js"></script>

Models

There's nothing that you'll need to do to make this work with models. The model id is going to be used to fetch/save the model and it will just work.

Collections

For collections, there's some extra configuration required:

Backbone.couch.Collection.extend({
    couch: function() {
        return {
            view: 'design/my_view'
        }
    },
    _db: Backbone.couch.db('my_database')
})

The couch function needs to return options to query the db with. In this example, The results of http://localhost:5984/my_database/_design/my_design_document/_view/my_view will be added to the collection (with each row being a separate model).

For a lot of views, it makes sense to have them look something like this:

emit(key, null);

This ends up saving disk space if you're just using the raw document (as it can be fetched with the include_docs query parameter). If you'd like to do this trick here:

{ view: 'design/my_view',
  include_docs: 'true'
}

Note that the couch function can return just about any query parameter. I find it especially useful for limiting the collection size and using startKey/endKey.

_changes

To get the changes feed working, you need to enable it in your model.

Backbone.couch.Collection.extend({
    change_feed: true
})

This is going to try and add everything on changes to your collection (which probably isn't the most desirable thing). To add filtering, so that the collection only sees updates for the specific thing it tracks, you can add a key to the couch() method.

Backbone.couch.Collection.extend({
    change_feed: true,
    couch: function() {
        return {
            filter: {
                filter: 'design/my_filter',
                /* query parameters */
                test: 'foobar'
            }
        }
    }
})

Update handlers

It is possible to use an update handler for all model create/updates. Just add the handler you'd like to use to the return value of couch():

Backbone.couch.Collection.extend({
    couch: function() {
        return {
            view: 'design/my_view',
            update: 'design/my_update'
        }
    }
})

Show handlers

If you'd like all the read calls to use a _show instead of the normal couch return for GET on documents. Like update handlers, you can just add the handler you'd like to use to the return value of couch():

Backbone.couch.Collection.extend({
    couch: function() {
        return {
            view: 'design/my_view',
            show: 'design/my_show'
        }
    }
})

List handlers

If you'd like your collections to use a _list instead of a _view, you can add the handler to couch():

Backbone.couch.Collection.extend({
    couch: function() {
        return {
            view: 'design/my_view',
            list: 'design/my_list'
        }
    }
})

Note that this will create an URL that looks like:

http://localhost:5984/mydb/_design/design/_list/my_list/my_view

TODOs

  • Take some time to explain a little more of why this is useful.
  • Do an example of the relationship between documents in the DB and models in the client.
  • Add some couchapp steps to show the setup in couchapp itself.
  • Show some more view options in the couch() method as examples.
  • Move best practice suggestion for views to a footnote.

More Repositories

1

janky.post

Breaking same-domain policy one request at a time
JavaScript
52
star
2

lein-autoreload

Keep the repl up to date with source.
Clojure
37
star
3

k8s-clusters

Simple kubernetes clusters on cloud providers for development
Makefile
25
star
4

k8s-egress

24
star
5

leaderboard

HTML
22
star
6

zeromq-cookbook

A chef cookbook to install zeromq
Ruby
19
star
7

python-flockdb

Python
9
star
8

mongrel2-cookbook

A chef cookbook for setting mongrel2 up
Ruby
6
star
9

scala-bencode

A navitve bencode library for scala
Scala
6
star
10

swarmstat

Statistics on the behavior of BitTorrent swarms.
Scala
5
star
11

sinew

Helpers for backbone.js
JavaScript
4
star
12

talks

3
star
13

k8s-icons

Python
3
star
14

couchqueue

A couchapp that converts CouchDB into a full featured message queue
JavaScript
3
star
15

saunter.org

Hosting for saunter.org
HTML
2
star
16

snark-maven

Building snark with maven instead of a Makefile
2
star
17

glue-cookbook

Ruby
2
star
18

jsload

Github branch of the awsome jsload library.
JavaScript
2
star
19

environment

My environment
Shell
1
star
20

klint

Open Policy Agent
1
star
21

firebug-lite

Branch of firebug lite 1.4 for IE specific fixes.
JavaScript
1
star
22

rsnd.org

Hosting of rsnd.org
1
star
23

scala-exercises

General scala exercises and scratch space.
Scala
1
star
24

keyczar-clj

A friendly clojure wrapper for keyczar
Clojure
1
star
25

mimic

Monitor what your javascript is doing
JavaScript
1
star
26

fling

CoffeeScript
1
star
27

project-euler

Python
1
star
28

keyczar

Crypto done right
Python
1
star
29

blog_engine

Python
1
star
30

meetup-attendance

JavaScript
1
star
31

confabulate

Two part interface between Amazon's SQS and arbitrary client's browsers
JavaScript
1
star
32

lrnd.net

lrnd.net
1
star
33

multi-cluster

Makefile
1
star
34

lift-hello

Some trial applications using lift to figure it out.
1
star
35

.js

My dotjs files.
JavaScript
1
star