PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/core/collection.js

https://github.com/ogrisel/VIE-2
JavaScript | 125 lines | 84 code | 23 blank | 18 comment | 17 complexity | 0b85e1ad62ec6eda99651d79f4ed9f46 MD5 | raw file
  1. // File: collection.js
  2. // Author: <a href="mailto:sebastian.germesin@dfki.de">Sebastian Germesin</a>
  3. //
  4. //just for convenience, should be removed in a later revision
  5. VIE.EntityManager.initializeCollection();
  6. //<strong>VIE2.EntityCollection</strong>: TODO: document me
  7. VIE2.EntityCollection = VIE.RDFEntityCollection.extend({
  8. //overwrite the internal _add method
  9. _add: function (model, opts) {
  10. if (!opts) { opts = {};}
  11. VIE.RDFEntityCollection.prototype._add.call(this, model, opts);
  12. //if the annotation does *not* come from the analyze() method
  13. //it comes from the user and hence,
  14. //we need to add the subject to the internal triple store.
  15. if (!opts.backend) {
  16. var triple = jQuery.rdf.triple(
  17. model.get('id'),
  18. 'a',
  19. 'owl:Thing',
  20. {namespaces: VIE2.namespaces}
  21. );
  22. VIE2.globalCache.add(triple);
  23. }
  24. //in any case, we query all connectors for the types of the entity.
  25. VIE2.lookup(model.get('id'), ['a'], function (m) {
  26. return function () {
  27. m.trigger('change:a');
  28. };
  29. }(model));
  30. },
  31. _remove: function (model, opts) {
  32. if (!opts) { opts = {};}
  33. if (model) {
  34. //when removing the model from this collection, that means
  35. //that we remove all corresponding data from the cache as well.
  36. if (VIE2.entities === this) {
  37. VIE2.removeFromCache(model.get('id'), '?x', '?y');
  38. //also remove from all other collections!
  39. jQuery.each(VIE2.mappings, function(k, v){
  40. v.collection.remove(model);
  41. });
  42. VIE.EntityManager.entities.remove(model, opts);
  43. }
  44. VIE.RDFEntityCollection.prototype._remove.call(this, model, opts);
  45. }
  46. }
  47. });
  48. VIE2.entities = new VIE2.EntityCollection();
  49. //<strong>VIE2.ObjectCollection</strong>: TODO: document me
  50. VIE2.ObjectCollection = Backbone.Collection.extend({
  51. _add: function (model, opts) {
  52. //TODO: propagate event to parent model
  53. if (!opts) { opts = {};}
  54. //adding a back-reference to the model
  55. model.collection = this;
  56. Backbone.Collection.prototype._add.call(this, model, opts);
  57. if (!opts.backend) {
  58. var triple = jQuery.rdf.triple(
  59. this.uri,
  60. this.property,
  61. model.tojQueryRdf(),
  62. {namespaces: VIE2.namespaces}
  63. );
  64. VIE2.globalCache.add(triple);
  65. if (this.parent) {
  66. this.parent.change();
  67. }
  68. }
  69. },
  70. _remove: function (model, opts) {
  71. if (model) {
  72. //remove corresponding triples
  73. VIE2.removeFromCache(this.uri, this.property, model.tojQueryRdf());
  74. Backbone.Collection.prototype._remove.call(this, model, opts);
  75. //update parent entity
  76. this.parent.change();
  77. }
  78. },
  79. getByValue: function (value, opts) {
  80. if (!opts) { opts = {}; }
  81. var found;
  82. $.each(this.models, function (i, model) {
  83. if (model.get('value') === value) {
  84. if (opts.lang) {
  85. if (opts.lang === model.get('lang')) {
  86. found = model;
  87. return false;
  88. }
  89. } else if (opts.datatype) {
  90. if (opts.datatype === model.get('datatype')) {
  91. found = model;
  92. return false;
  93. }
  94. } else {
  95. found = model;
  96. return false;
  97. }
  98. }
  99. });
  100. return found;
  101. }
  102. });