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

/sandbox/semantic-interaction/trunk/lib/vie2/core/collection.js

http://iks-project.googlecode.com/
JavaScript | 127 lines | 86 code | 23 blank | 18 comment | 17 complexity | 43b6ab42b0acb549641cccae39df1116 MD5 | raw file
Possible License(s): CC-BY-3.0
  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.toObj()}
  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'));
  38. delete VIE2.globalCache.databank.subjectIndex[model.get('id')]; //HACK: rdfQuery does not remove an entity from its internal DB when no other triples are present
  39. //also remove from all other collections!
  40. jQuery.each(VIE2.mappings, function(k, v){
  41. v.collection.remove(model);
  42. });
  43. VIE.EntityManager.entities.remove(model, opts);
  44. model.destroy();
  45. }
  46. VIE.RDFEntityCollection.prototype._remove.call(this, model, opts);
  47. }
  48. }
  49. });
  50. VIE2.entities = new VIE2.EntityCollection();
  51. //<strong>VIE2.ObjectCollection</strong>: TODO: document me
  52. VIE2.ObjectCollection = Backbone.Collection.extend({
  53. _add: function (model, opts) {
  54. //TODO: propagate event to parent model
  55. if (!opts) { opts = {};}
  56. //adding a back-reference to the model
  57. model.collection = this;
  58. Backbone.Collection.prototype._add.call(this, model, opts);
  59. if (!opts.backend) {
  60. var triple = jQuery.rdf.triple(
  61. this.uri,
  62. this.property,
  63. model.tojQueryRdf(),
  64. {namespaces: VIE2.namespaces.toObj()}
  65. );
  66. VIE2.globalCache.add(triple);
  67. if (this.parent) {
  68. this.parent.change();
  69. }
  70. }
  71. },
  72. _remove: function (model, opts) {
  73. if (model) {
  74. //remove corresponding triples
  75. VIE2.removeFromCache(this.uri, this.property, model.tojQueryRdf());
  76. Backbone.Collection.prototype._remove.call(this, model, opts);
  77. //update parent entity
  78. this.parent.change();
  79. }
  80. },
  81. getByValue: function (value, opts) {
  82. if (!opts) { opts = {}; }
  83. var found;
  84. $.each(this.models, function (i, model) {
  85. if (model.get('value') === value) {
  86. if (opts.lang) {
  87. if (opts.lang === model.get('lang')) {
  88. found = model;
  89. return false;
  90. }
  91. } else if (opts.datatype) {
  92. if (opts.datatype === model.get('datatype')) {
  93. found = model;
  94. return false;
  95. }
  96. } else {
  97. found = model;
  98. return false;
  99. }
  100. }
  101. });
  102. return found;
  103. }
  104. });