Permalink
Browse files

Handle corner case when saving multiple entities (including new ones)…

… to a slow server
  • Loading branch information...
1 parent e1f9980 commit 4ebebdb793798a32e4fb5011b2d8a6f98baedf7e @bergie committed Sep 21, 2012
Showing with 55 additions and 22 deletions.
  1. +28 −21 src/Collection.js
  2. +6 −1 src/view/EntityView.js
  3. +21 −0 test/service/rdfa.js
View
@@ -81,29 +81,36 @@ VIE.prototype.Collection = Backbone.Collection.extend({
newAttribs[attribute] = value;
return true;
}
- else if (existing.get(attribute) === value) {
- return true;
- } else {
- //merge existing attribute values with new ones!
- //not just overwrite 'em!!
- var oldVals = existing.attributes[attribute];
- var newVals = value;
- if (oldVals instanceof collection.vie.Collection) {
- // TODO: Merge collections
+
+ if (attribute === '@subject') {
+ if (model.isNew() && !existing.isNew()) {
+ // Save order issue, skip
return true;
}
- if (options.overrideAttributes) {
- newAttribs[attribute] = value;
- return true;
- }
- if (attribute === '@context') {
- newAttribs[attribute] = jQuery.extend(true, {}, oldVals, newVals);
- } else {
- oldVals = (jQuery.isArray(oldVals))? oldVals : [ oldVals ];
- newVals = (jQuery.isArray(newVals))? newVals : [ newVals ];
- newAttribs[attribute] = _.uniq(oldVals.concat(newVals));
- newAttribs[attribute] = (newAttribs[attribute].length === 1)? newAttribs[attribute][0] : newAttribs[attribute];
- }
+ }
+
+ if (existing.get(attribute) === value) {
+ return true;
+ }
+ //merge existing attribute values with new ones!
+ //not just overwrite 'em!!
+ var oldVals = existing.attributes[attribute];
+ var newVals = value;
+ if (oldVals instanceof collection.vie.Collection) {
+ // TODO: Merge collections
+ return true;
+ }
+ if (options.overrideAttributes) {
+ newAttribs[attribute] = value;
+ return true;
+ }
+ if (attribute === '@context') {
+ newAttribs[attribute] = jQuery.extend(true, {}, oldVals, newVals);
+ } else {
+ oldVals = (jQuery.isArray(oldVals))? oldVals : [ oldVals ];
+ newVals = (jQuery.isArray(newVals))? newVals : [ newVals ];
+ newAttribs[attribute] = _.uniq(oldVals.concat(newVals));
+ newAttribs[attribute] = (newAttribs[attribute].length === 1)? newAttribs[attribute][0] : newAttribs[attribute];
}
});
@@ -9,8 +9,9 @@ VIE.prototype.view.Entity = Backbone.View.extend({
this.vie = options.vie;
// Ensure view gets updated when properties of the Entity change.
- _.bindAll(this, 'render');
+ _.bindAll(this, 'render', 'renderAbout');
this.model.bind('change', this.render);
+ this.model.bind('change:@subject', this.renderAbout);
},
// Rendering a view means writing the properties of the Entity back to
@@ -23,5 +24,9 @@ VIE.prototype.view.Entity = Backbone.View.extend({
to(this.service).
execute();
return this;
+ },
+
+ renderAbout: function () {
+ this.vie.service(this.service).setElementSubject(this.model.getSubjectUri(), this.el);
}
});
View
@@ -159,7 +159,28 @@ test("Test collection reset with RDFa", function() {
ok(collection.isCollection);
equal(collection.length, 2);
equal(jQuery('li[about]', html).length, 2);
+ equal(jQuery('[about="http://example.net/collectionreset/item"]', html).length, 1);
+ equal(jQuery('[about="http://example.net/collectionreset/item2"]', html).length, 1);
+ // Add a blank node
+ collection.add({});
+ equal(jQuery('li[about]', html).length, 3);
+ var newItem = collection.at(2);
+ ok(newItem.isNew());
+ newItem.set('@subject', 'http://example.net/collectionreset/item3');
+ equal(jQuery('li[about]', html).length, 3);
+ equal(jQuery('[about="http://example.net/collectionreset/item"]', html).length, 1);
+ equal(jQuery('[about="http://example.net/collectionreset/item2"]', html).length, 1);
+ equal(jQuery('[about="http://example.net/collectionreset/item3"]', html).length, 1);
+
+ // Re-reset
+ entity.set({
+ collection: ['<http://example.net/collectionreset/item>', '<http://example.net/collectionreset/item2>', '<http://example.net/collectionreset/item3>']
+ });
+ equal(jQuery('li[about]', html).length, 3);
+ equal(jQuery('[about="http://example.net/collectionreset/item"]', html).length, 1);
+ equal(jQuery('[about="http://example.net/collectionreset/item2"]', html).length, 1);
+ equal(jQuery('[about="http://example.net/collectionreset/item3"]', html).length, 1);
start();
});
});

0 comments on commit 4ebebdb

Please sign in to comment.