PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Collection.js

https://github.com/elf-pavlik/VIE
JavaScript | 140 lines | 113 code | 15 blank | 12 comment | 28 complexity | 67406f6a776467d0a17c84008340a4e2 MD5 | raw file
  1. // VIE - Vienna IKS Editables
  2. // (c) 2011 Henri Bergius, IKS Consortium
  3. // (c) 2011 Sebastian Germesin, IKS Consortium
  4. // (c) 2011 Szaby Gr端nwald, IKS Consortium
  5. // VIE may be freely distributed under the MIT license.
  6. // For all details and documentation:
  7. // http://viejs.org/
  8. VIE.prototype.Collection = Backbone.Collection.extend({
  9. model: VIE.prototype.Entity,
  10. initialize: function (models, options) {
  11. if (!options || !options.vie) {
  12. throw new Error('Each collection needs a VIE reference');
  13. }
  14. this.vie = options.vie;
  15. this.predicate = options.predicate;
  16. },
  17. canAdd: function (type) {
  18. return true;
  19. },
  20. get: function(id) {
  21. if (id === null) {
  22. return null;
  23. }
  24. id = (id.getSubject)? id.getSubject() : id;
  25. if (typeof id === "string" && id.indexOf("_:") === 0) {
  26. if (id.indexOf("bnode") === 2) {
  27. //bnode!
  28. id = id.replace("_:bnode", 'c');
  29. return this._byId[id];
  30. } else {
  31. return this._byId["<" + id + ">"];
  32. }
  33. } else {
  34. if (this._byId[id]) {
  35. return this._byId[id];
  36. }
  37. id = this.toReference(id);
  38. return this._byId[id];
  39. }
  40. },
  41. addOrUpdate: function(model, options) {
  42. options = options || {};
  43. var collection = this;
  44. var existing;
  45. if (_.isArray(model)) {
  46. var entities = [];
  47. _.each(model, function(item) {
  48. entities.push(collection.addOrUpdate(item, options));
  49. });
  50. return entities;
  51. }
  52. if (model === undefined) {
  53. throw new Error("No model given");
  54. }
  55. if (_.isString(model)) {
  56. model = {
  57. '@subject': model,
  58. id: model
  59. };
  60. }
  61. if (!model.isEntity) {
  62. model = new this.model(model);
  63. }
  64. if (model.id && this.get(model.id)) {
  65. existing = this.get(model.id);
  66. }
  67. if (this.get(model.cid)) {
  68. existing = this.get(model.cid);
  69. }
  70. if (existing) {
  71. var newAttribs = {};
  72. _.each(model.attributes, function(value, attribute) {
  73. if (!existing.has(attribute)) {
  74. newAttribs[attribute] = value;
  75. return true;
  76. }
  77. if (attribute === '@subject') {
  78. if (model.isNew() && !existing.isNew()) {
  79. // Save order issue, skip
  80. return true;
  81. }
  82. }
  83. if (existing.get(attribute) === value) {
  84. return true;
  85. }
  86. //merge existing attribute values with new ones!
  87. //not just overwrite 'em!!
  88. var oldVals = existing.attributes[attribute];
  89. var newVals = value;
  90. if (oldVals instanceof collection.vie.Collection) {
  91. // TODO: Merge collections
  92. return true;
  93. }
  94. if (options.overrideAttributes) {
  95. newAttribs[attribute] = value;
  96. return true;
  97. }
  98. if (attribute === '@context') {
  99. newAttribs[attribute] = jQuery.extend(true, {}, oldVals, newVals);
  100. } else {
  101. oldVals = (jQuery.isArray(oldVals))? oldVals : [ oldVals ];
  102. newVals = (jQuery.isArray(newVals))? newVals : [ newVals ];
  103. newAttribs[attribute] = _.uniq(oldVals.concat(newVals));
  104. newAttribs[attribute] = (newAttribs[attribute].length === 1)? newAttribs[attribute][0] : newAttribs[attribute];
  105. }
  106. });
  107. if (!_.isEmpty(newAttribs)) {
  108. existing.set(newAttribs, options.updateOptions);
  109. }
  110. return existing;
  111. }
  112. this.add(model, options.addOptions);
  113. return model;
  114. },
  115. isReference: function (uri) {
  116. return VIE.Util.isReference(uri);
  117. },
  118. toReference: function (uri) {
  119. return VIE.Util.toReference(uri, this.vie.namespaces);
  120. },
  121. fromReference: function (uri) {
  122. return VIE.Util.fromReference(uri, this.vie.namespaces);
  123. },
  124. isCollection: true
  125. });