Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix handling of deep relations, fixes bergie/create#99

  • Loading branch information...
commit c0fcc845bb0a2e892786661c772dabf08840cd17 1 parent 35c4504
@bergie authored
View
10 src/Entity.js
@@ -113,6 +113,16 @@ VIE.prototype.Entity = function(attrs, opts) {
return Backbone.Model.prototype.has.call(this, attr);
},
+ hasRelations: function() {
+ var found = false;
+ _.each(this.attributes, function (value) {
+ if (value && value.isCollection) {
+ found = true;
+ }
+ });
+ return found;
+ },
+
// #### `.set(attrName, value, opts)`,
// The `options` parameter always refers to a `Backbone.Model.set` `options` object.
//
View
17 src/service/RdfaService.js
@@ -239,6 +239,11 @@ VIE.prototype.RdfaService.prototype = {
var service = this;
var viewInstance = this._getViewForElement(element);
if (viewInstance) {
+ if (entity.hasRelations() && !viewInstance.collectionsChecked) {
+ // Entity has collections but these haven't been registered
+ // as views yet. This usually happens with deep relations.
+ this._registerEntityCollectionViews(entity, element, viewInstance);
+ }
return viewInstance;
}
@@ -265,6 +270,14 @@ VIE.prototype.RdfaService.prototype = {
});
}
+ this._registerEntityCollectionViews(entity, element, viewInstance);
+
+ return viewInstance;
+ },
+
+
+ _registerEntityCollectionViews: function (entity, element, view) {
+ var service = this;
// Find collection elements and create collection views for them
_.each(entity.attributes, function(value, predicate) {
var attributeValue = entity.fromReference(entity.get(predicate));
@@ -272,9 +285,11 @@ VIE.prototype.RdfaService.prototype = {
jQuery.each(service.getElementByPredicate(predicate, element), function() {
service._registerCollectionView(attributeValue, jQuery(this), entity);
});
+ // Collections of the entity have been checked and views
+ // registered for them. This doesn't need to be done again.
+ view.collectionsChecked = true;
}
});
- return viewInstance;
},
setTemplate: function (type, predicate, template) {
View
7 src/view/CollectionView.js
@@ -22,10 +22,9 @@ VIE.prototype.view.Collection = Backbone.View.extend({
this.collection.on('reset', this.refreshItems);
// Make the view aware of existing entities in collection
- var view = this;
- this.collection.forEach(function(entity) {
- view.registerItem(entity, view.collection);
- });
+ this.collection.each(function(entity) {
+ this.registerItem(entity, this.collection);
+ }, this);
},
/*
View
15 test/index.html
@@ -192,6 +192,21 @@ <h2 property="title">Content</h2>
</div>
</div>
</div>
+ <div class="rdfa-deeprelation">
+ <div about="http://example.net/example">
+ <div property="dcterms:title">Foo</div>
+ <div rel="relations">
+ <div about="http://example.net/first">
+ <div property="dcterms:title">Bar</div>
+ <div rel="subrelations">
+ <div about="http://example.net/second">
+ <div property="dcterms:title">Baz</div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
</div>
<!--<h1 id="qunit-header"><a href="#">vie.js Speed Suite</a></h1>
View
63 test/service/rdfa.js
@@ -450,3 +450,66 @@ test("Test anonymous relation", function () {
equal(relations.at(0).get('dcterms:title'), 'Bar');
});
});
+
+test("Test deep relations", function () {
+ var z = new VIE();
+ z.use(new z.RdfaService);
+
+ var html = jQuery('#qunit-fixture .rdfa-deeprelation');
+
+ stop();
+ z.load({element: html}).from('rdfa').execute().done(function (entities) {
+ start();
+
+ var entity = z.entities.get('<http://example.net/example>');
+ ok(entity);
+ equal(entity.get('dcterms:title'), 'Foo');
+
+ equal(jQuery('div[about]', html).length, 3);
+
+ var relations = entity.get('relations');
+ equal(relations.length, 1);
+
+ var first = relations.at(0);
+ equal(first.get('dcterms:title'), 'Bar');
+ equal(jQuery('[rel="relations"] > div[about]', html).length, 1);
+
+ relations.add({
+ 'dcterms:title': 'BarFoo'
+ });
+ equal(relations.length, 2);
+ equal(relations.at(1).get('dcterms:title'), 'BarFoo');
+ equal(jQuery('[rel="relations"] > div[about]', html).length, 2);
+ equal(jQuery('div[property]', jQuery('[rel="relations"] > div[about]', html).get(1)).html(), 'BarFoo');
+
+ relations.at(1).set('dcterms:title', 'BarFooBaz');
+ equal(jQuery('div[property]', jQuery('[rel="relations"] > div[about]', html).get(1)).html(), 'BarFooBaz');
+
+ relations.remove(relations.at(1));
+ equal(relations.length, 1);
+ equal(jQuery('[rel="relations"] > div[about]', html).length, 1);
+ equal(jQuery('[rel="relations"] > div[about] div[property]', html).html(), 'Bar');
+
+ var subrelations = first.get('subrelations');
+ equal(subrelations.length, 1);
+ var second = subrelations.at(0);
+ equal(second.get('dcterms:title'), 'Baz');
+
+ equal(jQuery('[rel="subrelations"] div[about]', html).length, 1);
+
+ subrelations.add({
+ 'dcterms:title': 'BazFoo'
+ });
+ equal(subrelations.length, 2);
+ equal(jQuery('[rel="subrelations"] div[about]', html).length, 2);
+ equal(jQuery('div[property]', jQuery('[rel="subrelations"] div[about]', html).get(1)).html(), 'BazFoo');
+
+ subrelations.at(1).set('dcterms:title', 'BazFooBar');
+ equal(jQuery('div[property]', jQuery('[rel="subrelations"] > div[about]', html).get(1)).html(), 'BazFooBar');
+
+ subrelations.remove(subrelations.at(1));
+ equal(subrelations.length, 1);
+ equal(jQuery('[rel="subrelations"] div[about]', html).length, 1);
+ equal(jQuery('[rel="subrelations"] > div[about] div[property]', html).html(), 'Baz');
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.