PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/_attachments/js/lib/backbone-couchdb.js

https://bitbucket.org/nicoechaniz/altermap
JavaScript | 340 lines | 310 code | 24 blank | 6 comment | 76 complexity | eba352174fd32171c68dff01eac2303a MD5 | raw file
  1. // Generated by CoffeeScript 1.3.1
  2. /*
  3. (c) 2011 Jan Monschke
  4. v1.2.2
  5. backbone-couchdb.js is licensed under the MIT license.
  6. */
  7. var con,
  8. __hasProp = {}.hasOwnProperty,
  9. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; },
  10. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  11. Backbone.couch_connector = con = {
  12. config: {
  13. db_name: "backbone_connect",
  14. ddoc_name: "backbone_example",
  15. view_name: "byCollection",
  16. list_name: null,
  17. global_changes: false,
  18. base_url: null
  19. },
  20. helpers: {
  21. extract_collection_name: function(model) {
  22. var _name, _splitted;
  23. if (model == null) {
  24. throw new Error("No model has been passed");
  25. }
  26. if (!(((model.collection != null) && (model.collection.url != null)) || (model.url != null))) {
  27. return "";
  28. }
  29. if (model.url != null) {
  30. _name = _.isFunction(model.url) ? model.url() : model.url;
  31. } else {
  32. _name = _.isFunction(model.collection.url) ? model.collection.url() : model.collection.url;
  33. }
  34. if (_name[0] === "/") {
  35. _name = _name.slice(1, _name.length);
  36. }
  37. _splitted = _name.split("/");
  38. _name = _splitted.length > 0 ? _splitted[0] : _name;
  39. _name = _name.replace("/", "");
  40. return _name;
  41. },
  42. make_db: function() {
  43. var db;
  44. db = $.couch.db(con.config.db_name);
  45. if (con.config.base_url != null) {
  46. db.uri = "" + con.config.base_url + "/" + con.config.db_name + "/";
  47. }
  48. return db;
  49. }
  50. },
  51. read: function(model, opts) {
  52. if (model.models) {
  53. return con.read_collection(model, opts);
  54. } else {
  55. return con.read_model(model, opts);
  56. }
  57. },
  58. read_collection: function(coll, opts) {
  59. var keys, option, view_options, _ddoc, _i, _len, _list, _opts, _view,
  60. _this = this;
  61. _view = this.config.view_name;
  62. _ddoc = this.config.ddoc_name;
  63. _list = this.config.list_name;
  64. keys = [this.helpers.extract_collection_name(coll)];
  65. if (coll.db != null) {
  66. if (coll.db.changes || this.config.global_changes) {
  67. coll.listen_to_changes();
  68. }
  69. if (coll.db.view != null) {
  70. _view = coll.db.view;
  71. }
  72. if (coll.db.ddoc != null) {
  73. _ddoc = coll.db.ddoc;
  74. }
  75. if (coll.db.keys != null) {
  76. keys = coll.db.keys;
  77. }
  78. if (coll.db.list != null) {
  79. _list = coll.db.list;
  80. }
  81. }
  82. _opts = {
  83. keys: keys,
  84. success: function(data) {
  85. var doc, _i, _len, _ref, _temp;
  86. _temp = [];
  87. _ref = data.rows;
  88. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  89. doc = _ref[_i];
  90. if (doc.value) {
  91. _temp.push(doc.value);
  92. } else {
  93. _temp.push(doc.doc);
  94. }
  95. }
  96. opts.success(_temp);
  97. return opts.complete();
  98. },
  99. error: function(status, error, reason) {
  100. var res;
  101. res = {
  102. status: status,
  103. error: error,
  104. reason: reason
  105. };
  106. opts.error(res);
  107. return opts.complete(res);
  108. }
  109. };
  110. view_options = ["key", "keys", "startkey", "startkey_docid", "endkey", "endkey_docid", "limit", "stale", "descending", "skip", "group", "group_level", "reduce", "include_docs", "inclusive_end", "update_seq"];
  111. for (_i = 0, _len = view_options.length; _i < _len; _i++) {
  112. option = view_options[_i];
  113. if (opts[option] != null) {
  114. _opts[option] = opts[option];
  115. }
  116. }
  117. if ((coll.db != null) && (coll.db.view != null) && !(coll.db.keys != null)) {
  118. delete _opts.keys;
  119. }
  120. if (_list) {
  121. return this.helpers.make_db().list("" + _ddoc + "/" + _list, "" + _view, _opts);
  122. } else {
  123. return this.helpers.make_db().view("" + _ddoc + "/" + _view, _opts);
  124. }
  125. },
  126. read_model: function(model, opts) {
  127. if (!model.id) {
  128. throw new Error("The model has no id property, so it can't get fetched from the database");
  129. }
  130. return this.helpers.make_db().openDoc(model.id, {
  131. success: function(doc) {
  132. opts.success(doc);
  133. return opts.complete();
  134. },
  135. error: function(status, error, reason) {
  136. var res;
  137. res = {
  138. status: status,
  139. error: error,
  140. reason: reason
  141. };
  142. opts.error(res);
  143. return opts.complete(res);
  144. }
  145. });
  146. },
  147. create: function(model, opts) {
  148. var coll, vals;
  149. vals = model.toJSON();
  150. coll = this.helpers.extract_collection_name(model);
  151. if (coll.length > 0) {
  152. vals.collection = coll;
  153. }
  154. return this.helpers.make_db().saveDoc(vals, {
  155. success: function(doc) {
  156. opts.success({
  157. _id: doc.id,
  158. _rev: doc.rev
  159. });
  160. return opts.complete();
  161. },
  162. error: function(status, error, reason) {
  163. var res;
  164. res = {
  165. status: status,
  166. error: error,
  167. reason: reason
  168. };
  169. opts.error(res);
  170. return opts.complete(res);
  171. }
  172. });
  173. },
  174. update: function(model, opts) {
  175. return this.create(model, opts);
  176. },
  177. del: function(model, opts) {
  178. return this.helpers.make_db().removeDoc(model.toJSON(), {
  179. success: function() {
  180. return opts.success();
  181. },
  182. error: function(nr, req, e) {
  183. var res;
  184. if (e === "deleted") {
  185. opts.success();
  186. return opts.complete();
  187. } else {
  188. res = {
  189. status: status,
  190. error: error,
  191. reason: reason
  192. };
  193. opts.error(res);
  194. return opts.complete(res);
  195. }
  196. }
  197. });
  198. }
  199. };
  200. Backbone.sync = function(method, model, opts) {
  201. if (opts.success == null) {
  202. opts.success = function() {};
  203. }
  204. if (opts.error == null) {
  205. opts.error = function() {};
  206. }
  207. if (opts.complete == null) {
  208. opts.complete = function() {};
  209. }
  210. switch (method) {
  211. case "read":
  212. return con.read(model, opts);
  213. case "create":
  214. return con.create(model, opts);
  215. case "update":
  216. return con.update(model, opts);
  217. case "delete":
  218. return con.del(model, opts);
  219. }
  220. };
  221. Backbone.Model = (function(_super) {
  222. __extends(Model, _super);
  223. Model.name = 'Model';
  224. function Model() {
  225. return Model.__super__.constructor.apply(this, arguments);
  226. }
  227. Model.prototype.idAttribute = "_id";
  228. Model.prototype.clone = function() {
  229. var new_model;
  230. new_model = new this.constructor(this);
  231. if (new_model.attributes._id) {
  232. delete new_model.attributes._id;
  233. }
  234. if (new_model.attributes._rev) {
  235. delete new_model.attributes._rev;
  236. }
  237. return new_model;
  238. };
  239. return Model;
  240. })(Backbone.Model);
  241. Backbone.Collection = (function(_super) {
  242. __extends(Collection, _super);
  243. Collection.name = 'Collection';
  244. function Collection() {
  245. this._db_on_change = __bind(this._db_on_change, this);
  246. this._db_prepared_for_changes = __bind(this._db_prepared_for_changes, this);
  247. return Collection.__super__.constructor.apply(this, arguments);
  248. }
  249. Collection.prototype.model = Backbone.Model;
  250. Collection.prototype.initialize = function() {
  251. if (!this._db_changes_enabled && ((this.db && this.db.changes) || con.config.global_changes)) {
  252. return this.listen_to_changes();
  253. }
  254. };
  255. Collection.prototype.listen_to_changes = function() {
  256. if (!this._db_changes_enabled) {
  257. this._db_changes_enabled = true;
  258. if (!this._db_inst) {
  259. this._db_inst = con.helpers.make_db();
  260. }
  261. return this._db_inst.info({
  262. "success": this._db_prepared_for_changes
  263. });
  264. }
  265. };
  266. Collection.prototype.stop_changes = function() {
  267. this._db_changes_enabled = false;
  268. if (this._db_changes_handler != null) {
  269. this._db_changes_handler.stop();
  270. return this._db_changes_handler = null;
  271. }
  272. };
  273. Collection.prototype._db_prepared_for_changes = function(data) {
  274. var opts,
  275. _this = this;
  276. this._db_update_seq = data.update_seq || 0;
  277. opts = {
  278. include_docs: true,
  279. collection: con.helpers.extract_collection_name(this),
  280. filter: "" + con.config.ddoc_name + "/by_collection"
  281. };
  282. _.extend(opts, this.db);
  283. return _.defer(function() {
  284. _this._db_changes_handler = _this._db_inst.changes(_this._db_update_seq, opts);
  285. return _this._db_changes_handler.onChange(_this._db_on_change);
  286. });
  287. };
  288. Collection.prototype._db_on_change = function(changes) {
  289. var obj, _doc, _i, _len, _ref, _results;
  290. _ref = changes.results;
  291. _results = [];
  292. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  293. _doc = _ref[_i];
  294. obj = this.get(_doc.id);
  295. if (obj != null) {
  296. if (_doc.deleted) {
  297. _results.push(this.remove(obj));
  298. } else {
  299. if (obj.get("_rev") !== _doc.doc._rev) {
  300. _results.push(obj.set(_doc.doc));
  301. } else {
  302. _results.push(void 0);
  303. }
  304. }
  305. } else {
  306. if (!_doc.deleted) {
  307. _results.push(this.add(_doc.doc));
  308. } else {
  309. _results.push(void 0);
  310. }
  311. }
  312. }
  313. return _results;
  314. };
  315. return Collection;
  316. })(Backbone.Collection);