PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/js/systemtags/systemtagscollection.js

https://gitlab.com/wuhang2003/core
JavaScript | 89 lines | 45 code | 14 blank | 30 comment | 6 complexity | 26a1ae1430c855146211e301228ca1ad MD5 | raw file
  1. /*
  2. * Copyright (c) 2015
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function(OC) {
  11. function filterFunction(model, term) {
  12. return model.get('name').substr(0, term.length).toLowerCase() === term.toLowerCase();
  13. }
  14. /**
  15. * @class OCA.SystemTags.SystemTagsCollection
  16. * @classdesc
  17. *
  18. * Collection of tags assigned to a file
  19. *
  20. */
  21. var SystemTagsCollection = OC.Backbone.Collection.extend(
  22. /** @lends OC.SystemTags.SystemTagsCollection.prototype */ {
  23. sync: OC.Backbone.davSync,
  24. model: OC.SystemTags.SystemTagModel,
  25. url: function() {
  26. return OC.linkToRemote('dav') + '/systemtags/';
  27. },
  28. filterByName: function(name) {
  29. return this.filter(function(model) {
  30. return filterFunction(model, name);
  31. });
  32. },
  33. reset: function() {
  34. this.fetched = false;
  35. return OC.Backbone.Collection.prototype.reset.apply(this, arguments);
  36. },
  37. /**
  38. * Lazy fetch.
  39. * Only fetches once, subsequent calls will directly call the success handler.
  40. *
  41. * @param options
  42. * @param [options.force] true to force fetch even if cached entries exist
  43. *
  44. * @see Backbone.Collection#fetch
  45. */
  46. fetch: function(options) {
  47. var self = this;
  48. options = options || {};
  49. if (this.fetched || options.force) {
  50. // directly call handler
  51. if (options.success) {
  52. options.success(this, null, options);
  53. }
  54. // trigger sync event
  55. this.trigger('sync', this, null, options);
  56. return Promise.resolve();
  57. }
  58. var success = options.success;
  59. options = _.extend({}, options);
  60. options.success = function() {
  61. self.fetched = true;
  62. if (success) {
  63. return success.apply(this, arguments);
  64. }
  65. };
  66. return OC.Backbone.Collection.prototype.fetch.call(this, options);
  67. }
  68. });
  69. OC.SystemTags = OC.SystemTags || {};
  70. OC.SystemTags.SystemTagsCollection = SystemTagsCollection;
  71. /**
  72. * @type OC.SystemTags.SystemTagsCollection
  73. */
  74. OC.SystemTags.collection = new OC.SystemTags.SystemTagsCollection();
  75. })(OC);