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

Language Javascript Lines 128
MD5 Hash 43b6ab42b0acb549641cccae39df1116
Repository http://iks-project.googlecode.com/svn/ View Raw File
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// File:   collection.js
// Author: <a href="mailto:sebastian.germesin@dfki.de">Sebastian Germesin</a>
//

//just for convenience, should be removed in a later revision
VIE.EntityManager.initializeCollection();

//<strong>VIE2.EntityCollection</strong>: TODO: document me
VIE2.EntityCollection = VIE.RDFEntityCollection.extend({
    
    //overwrite the internal _add method
    _add: function (model, opts) {
        if (!opts) { opts = {};}
        VIE.RDFEntityCollection.prototype._add.call(this, model, opts);
        
        //if the annotation does *not* come from the analyze() method
        //it comes from the user and hence,
        //we need to add the subject to the internal triple store.
        if (!opts.backend) {
            var triple = jQuery.rdf.triple(
                model.get('id'), 
                'a', 
                'owl:Thing', 
                {namespaces: VIE2.namespaces.toObj()}
            );
            VIE2.globalCache.add(triple);    
        }
        //in any case, we query all connectors for the types of the entity.
        VIE2.lookup(model.get('id'), ['a'], function (m) {
            return function () {
                m.trigger('change:a');
            };
        }(model));
    },
    
    _remove: function (model, opts) {
        if (!opts) { opts = {};}
        if (model) {
            //when removing the model from this collection, that means
            //that we remove all corresponding data from the cache as well.
            if (VIE2.entities === this) {
                VIE2.removeFromCache(model.get('id'));
                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
                //also remove from all other collections!
                jQuery.each(VIE2.mappings, function(k, v){
                    v.collection.remove(model);
                });
                
                VIE.EntityManager.entities.remove(model, opts);
                model.destroy();
            }
            VIE.RDFEntityCollection.prototype._remove.call(this, model, opts);
        }
    }
});

VIE2.entities = new VIE2.EntityCollection();

//<strong>VIE2.ObjectCollection</strong>: TODO: document me
VIE2.ObjectCollection = Backbone.Collection.extend({
        
    _add: function (model, opts) {
        //TODO: propagate event to parent model
        if (!opts) { opts = {};}
        
        //adding a back-reference to the model
        model.collection = this;
        Backbone.Collection.prototype._add.call(this, model, opts);
        
        if (!opts.backend) {
            var triple = jQuery.rdf.triple(
                this.uri, 
                this.property, 
                model.tojQueryRdf(), 
                {namespaces: VIE2.namespaces.toObj()}
            );
            VIE2.globalCache.add(triple);
            if (this.parent) {
                this.parent.change();
            }
        }
    },
    
     _remove: function (model, opts) {
         if (model) {
             //remove corresponding triples
            VIE2.removeFromCache(this.uri, this.property, model.tojQueryRdf());
            
            Backbone.Collection.prototype._remove.call(this, model, opts);
             
            //update parent entity
            this.parent.change();
        }
     },
     
     getByValue: function (value, opts) {
         if (!opts) { opts = {}; }
         
         var found;
         $.each(this.models, function (i, model) {
             if (model.get('value') === value) {
                 if (opts.lang) {
                     if (opts.lang === model.get('lang')) {
                         found = model;
                         return false;
                     }
                 } else if (opts.datatype) {
                     if (opts.datatype === model.get('datatype')) {
                         found = model;
                         return false;
                     }
                 } else {
                     found = model;
                     return false;
                 }
             }
         });
         
         return found;
     }
});
Back to Top