Syncpoint-API /lib/design/console/vendor/backbone.couchdb.js

Language Javascript Lines 182
MD5 Hash 04e780a5601d76e8312bae10090780a0
Repository git://github.com/couchbaselabs/Syncpoint-API.git View Raw File
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
 * @author: Thomas Rampelberg <thomas@saunter.org>
 *
 * Copyright(c) 2011 Thomas Rampelberg
 */

(function($) {

  Backbone.couch = {};
  Backbone.couch.options = {
    database: 'test',
    design: 'backbone'
  };

  Backbone.couch.utils = {
    _format_row: function(row) {
      _.extend(row, row.value ? { value: row.value } : {}, row.doc);
      // if (!row.id && row._id) row.id = row._id;

      // Since we reassigned these, remove them from the object;
      // delete row._id;
      delete row.doc;

      // if (!row.id) row.id = k;
      return row;
    },
    _ignores: [ 'view', 'update', 'filter', 'show', 'list' ],
    _remove: function(opts) {
      _.each(couch._ignores, function(v) {
        delete opts[v];
      });
    },
    model: {
      create: function(_db, model, cb) {
        var opts = (model.couch && model.couch()) || {};
        var method = _.bind(_db.saveDoc, _db);
        if ('update' in opts)
          method = _.bind(_db.updateDoc, _db, opts.update);

        couch._remove(opts);
        method(model.toJSON(), _.extend(opts, {
          success: function(resp) {
            cb.success(_.extend({
              _id: resp.id,
              _rev: resp.rev
            }, resp.doc));
          },
          error: cb.error
        }));
      },
      update: function(_db, model, cb) {
        this.create(_db, model, cb);
      },
      read: function(_db, model, cb) {
        var opts = (model.couch && model.couch()) || {};
        var method = _.bind(_db.openDoc, _db);
        if ('show' in opts)
          method = _.bind(_db.show, _db, opts.show);

        method(model.id, {
          success: cb.success,
          error: cb.error
        });
      },
      'delete': function(_db, model, cb) {
        _db.removeDoc(_.extend({ _id: model.id }, model.toJSON()), {
          success: cb.success,
          error: cb.error
        });
      }
    },
    collection: {
      read: function(_db, model, cb) {
        var opts = model.couch();
        var method = null;
        if ('view' in opts)
          method = _.bind(_db.view, _db, opts.view);
        if ('list' in opts)
          method = _.bind(_db.list, _db, opts.list, opts.view.split('/')[1]);

        if (!method)
          throw new Error("The return of `couch()` must contain the view " +
                          "and/or the list");

        couch._remove(opts);
        method(_.extend({
          success: function(resp) {
            cb.success(_.map(resp.rows, couch._format_row));
          },
          error: cb.error
        }, opts));
      }
    },
    sync: function(method, model, cb) {
      // XXX - This is going to be a memory issue unless someone does the
      // extend trick.
      var _db = this._db || (this.collection && this.collection._db);
      if (!_db) _db = this._db = Backbone.couch.db(
        Backbone.couch.options.database);
      var type = 'model' in model ? 'collection' : 'model';
      couch[type][method](_db, model, cb);
    }
  };

  Backbone.couch.db = function(name, db_opts) {
    if (!db_opts) db_opts = {};
    var old_prefix = $.couch.urlPrefix;
    $.couch.urlPrefix = db_opts.host || $.couch.urlPrefix;

    var ret = $.couch.db(name, db_opts);

    var base_func = function(path, fname, handler) {
      var current_uri = this.uri;
      
      var name = handler.split('/');
      this.uri = this.uri + '_design/' + name[0] + '/_' + path + '/' + name[1] +
        '/';
      this[fname].apply(this, _.toArray(arguments).slice(3));
      
      this.uri = current_uri;
    };

    _.extend(ret, {
      show: _.bind(base_func, ret, 'show', 'openDoc'),
      updateDoc: _.bind(base_func, ret, 'update', 'saveDoc')
    });
    $.couch.urlPrefix = old_prefix;
    return ret
  };

  var couch = Backbone.couch.utils;

  Backbone.couch.sync = couch.sync;

  Backbone.couch.Model = Backbone.Model.extend({
    sync: couch.sync,
    idAttribute: '_id'
  });

  Backbone.couch.Collection = Backbone.Collection.extend({
    model: Backbone.couch.Model,
    sync: couch.sync,
    change_feed: false,
    initialize: function() {
      _.bindAll(this, '_init_changes', '_on_change');

      if (this.change_feed)
        this._db.info({
          success: this._init_changes
        });
    },
    _init_changes: function(state) {
      var seq = state.update_seq || 0;

      var opts = { include_docs: true };
      var col_opts = this.couch();
      if ('filter' in col_opts)
        _.extend(opts, col_opts.filter);

      this._changes = this._db.changes(seq, opts);
      this._changes.onChange(this._on_change);
    },
    _on_change: function(changes) {
      var _this = this;
      _.each(changes.results, function(res) {
        var client_model = _this.get(res.id);

        if (client_model) {
          if (res.deleted) return _this.remove(client_model);
          if (client_model.get('_rev') == res.doc._rev) return

          return client_model.set(res.doc);
        }

        if (res.deleted) return
        _this.add(couch._format_row(res));
      });
    }
  });

})(jQuery);
Back to Top