/src/Collection.js

https://github.com/neogermi/VIE · JavaScript · 129 lines · 105 code · 13 blank · 11 comment · 28 complexity · 1b518be40b380166498b14429ade588f 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. get: function(id) {
  11. if (id === null) {
  12. return null;
  13. }
  14. id = (id.getSubject)? id.getSubject() : id;
  15. if (typeof id === "string" && id.indexOf("_:") === 0) {
  16. if (id.indexOf("bnode") === 2) {
  17. //bnode!
  18. id = id.replace("_:bnode", 'c');
  19. return this._byCid[id];
  20. } else {
  21. return this._byId["<" + id + ">"];
  22. }
  23. } else {
  24. id = this.toReference(id);
  25. return this._byId[id];
  26. }
  27. },
  28. addOrUpdate: function(model, options) {
  29. options || (options = {});
  30. var collection = this;
  31. var existing;
  32. if (_.isArray(model)) {
  33. var entities = [];
  34. _.each(model, function(item) {
  35. entities.push(collection.addOrUpdate(item, options));
  36. });
  37. return entities;
  38. }
  39. if (model === undefined) {
  40. throw new Error("No model given");
  41. }
  42. if (_.isString(model) && collection.isReference(model)) {
  43. model = {
  44. '@subject': model
  45. };
  46. }
  47. if (!model.isEntity) {
  48. model = new this.model(model);
  49. }
  50. if (model.id && this.get(model.id)) {
  51. existing = this.get(model.id);
  52. }
  53. if (this.getByCid(model.cid)) {
  54. var existing = this.getByCid(model.cid);
  55. }
  56. if (existing) {
  57. var newAttribs = {};
  58. _.each(model.attributes, function(value, attribute) {
  59. if (!existing.has(attribute)) {
  60. newAttribs[attribute] = value;
  61. return true;
  62. }
  63. else if (existing.get(attribute) === value) {
  64. return true;
  65. } else {
  66. //merge existing attribute values with new ones!
  67. //not just overwrite 'em!!
  68. var oldVals = existing.attributes[attribute];
  69. var newVals = value;
  70. if (oldVals instanceof collection.vie.Collection) {
  71. // TODO: Merge collections
  72. return true;
  73. }
  74. if (options.overrideAttributes) {
  75. newAttribs[attribute] = value;
  76. return true;
  77. }
  78. if (attribute === '@context') {
  79. newAttribs[attribute] = jQuery.extend(true, {}, oldVals, newVals);
  80. } else {
  81. oldVals = (jQuery.isArray(oldVals))? oldVals : [ oldVals ];
  82. newVals = (jQuery.isArray(newVals))? newVals : [ newVals ];
  83. newAttribs[attribute] = _.uniq(oldVals.concat(newVals));
  84. newAttribs[attribute] = (newAttribs[attribute].length === 1)? newAttribs[attribute][0] : newAttribs[attribute];
  85. }
  86. }
  87. });
  88. if (!_.isEmpty(newAttribs)) {
  89. existing.set(newAttribs, options.updateOptions);
  90. }
  91. return existing;
  92. }
  93. this.add(model, options.addOptions);
  94. return model;
  95. },
  96. isReference: function(uri){
  97. var matcher = new RegExp("^\\<([^\\>]*)\\>$");
  98. if (matcher.exec(uri)) {
  99. return true;
  100. }
  101. return false;
  102. },
  103. toReference: function(uri){
  104. if (this.isReference(uri)) {
  105. return uri;
  106. }
  107. return '<' + uri + '>';
  108. },
  109. fromReference: function(uri){
  110. if (!this.isReference(uri)) {
  111. return uri;
  112. }
  113. return uri.substring(1, uri.length - 1);
  114. },
  115. isCollection: true
  116. });