PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/scripts/collections/removed.js

https://gitlab.com/dannywillems/laverna
JavaScript | 65 lines | 43 code | 11 blank | 11 comment | 3 complexity | 2ba62524f50c5f46dc9ac2f816f86737 MD5 | raw file
  1. /*global define*/
  2. define([
  3. 'jquery',
  4. 'backbone',
  5. 'migrations/note',
  6. 'models/removed',
  7. ], function ($, Backbone, NotesDB, Removed) {
  8. 'use strict';
  9. /**
  10. * Collection of IDs of removed objects
  11. * We use this collection for synchronizing purposes
  12. */
  13. var RemovedObjects = Backbone.Collection.extend({
  14. model: Removed,
  15. database: NotesDB,
  16. storeName: 'removed',
  17. getID: function (model) {
  18. return this.get(model.storeName + '/' + model.id);
  19. },
  20. /**
  21. * Returns filtered collection of IDs
  22. */
  23. filterIt: function (object) {
  24. var store;
  25. return this.filter(function (model) {
  26. store = model.id.split('/');
  27. return store[0] === object.storeName;
  28. });
  29. },
  30. /**
  31. * Stores id of a model, then destroys it
  32. */
  33. newObject: function (object, options) {
  34. var done = $.Deferred(),
  35. model = this.create({
  36. id: object.storeName + '/' + object.id
  37. });
  38. options = options || {};
  39. $.when(model.save(), object.destroy()).then(function () {
  40. done.resolve();
  41. if (typeof options.success === 'function') {
  42. options.success(object);
  43. }
  44. }, function () {
  45. done.fail();
  46. if (typeof options.error === 'function') {
  47. options.error(object);
  48. }
  49. });
  50. return model.save();
  51. }
  52. });
  53. return RemovedObjects;
  54. });