PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/scripts/collections/files.js

https://gitlab.com/dannywillems/laverna
JavaScript | 82 lines | 64 code | 14 blank | 4 comment | 4 complexity | 1f8f88ced8693fc783f8a919ed00e34c MD5 | raw file
  1. /*global define*/
  2. define([
  3. 'underscore',
  4. 'jquery',
  5. 'backbone',
  6. 'models/file',
  7. 'migrations/note',
  8. 'toBlob'
  9. ], function (_, $, Backbone, File, FileDB, toBlob) {
  10. 'use strict';
  11. /**
  12. * Files collection
  13. */
  14. var Files = Backbone.Collection.extend({
  15. model: File,
  16. database : FileDB,
  17. storeName: 'files',
  18. initialize: function () {
  19. },
  20. fetchImages: function (images) {
  21. var d = $.Deferred(),
  22. self = this,
  23. model;
  24. if (!images || images.length === 0) {
  25. d.resolve();
  26. }
  27. _.forEach(images, function (img, index) {
  28. model = new File({id: img});
  29. model.fetch({
  30. success: function (model) {
  31. model.set('src', toBlob(model.get('src')));
  32. self.add(model);
  33. if (index === (images.length - 1) ) {
  34. d.resolve(self);
  35. }
  36. },
  37. error: function (e) {
  38. d.error(e);
  39. }
  40. });
  41. });
  42. return d;
  43. },
  44. uploadImages: function (imgs) {
  45. var d = $.Deferred(),
  46. self = this,
  47. models = [],
  48. model;
  49. _.forEach(imgs, function (img, index) {
  50. model = new File();
  51. model.set(img);
  52. self.create(model, {
  53. success: function (m) {
  54. models.push(m);
  55. if (index === ( imgs.length - 1 )) {
  56. d.resolve(models);
  57. }
  58. },
  59. error: function (e) {
  60. d.reject(e);
  61. }
  62. });
  63. });
  64. return d;
  65. }
  66. });
  67. return Files;
  68. });