Permalink
Browse files

Don't record unnecessary changes when parsing entities from DOM

  • Loading branch information...
1 parent e981d35 commit 4e91550ad792c3c8f38677b812371f7c881c3178 @bergie committed Nov 29, 2012
Showing with 40 additions and 2 deletions.
  1. +6 −1 src/Entity.js
  2. +2 −1 src/service/RdfaService.js
  3. +8 −0 test/index.html
  4. +24 −0 test/service/rdfa.js
View
@@ -191,7 +191,12 @@ VIE.prototype.Entity = function(attrs, opts) {
}
}
}, this);
- return Backbone.Model.prototype.set.call(this, attrs, options);
+ var ret = Backbone.Model.prototype.set.call(this, attrs, options);
+ if (options && options.ignoreChanges) {
+ this.changed = {};
+ this._previousAttributes = _.clone(this.attributes);
+ }
+ return ret;
},
// **`.unset(attr, opts)` ** removes an attribute from the entity.
@@ -183,7 +183,8 @@ VIE.prototype.RdfaService.prototype = {
var entityInstance = new this.vie.Entity(entity);
entityInstance = this.vie.entities.addOrUpdate(entityInstance, {
updateOptions: {
- silent: true
+ silent: true,
+ ignoreChanges: true
}
});
this._registerEntityView(entityInstance, element);
View
@@ -174,6 +174,14 @@ <h2 property="title">Content</h2>
<div property="number" datatype="xsd:integer">123</div>
</div>
</div>
+ <div class="rdfa-twoinstances">
+ <div about="http://example.net/example">
+ <div property="dcterms:title">Foo</div>
+ </div>
+ <div about="http://example.net/example">
+ <div property="dcterms:description">Bar</div>
+ </div>
+ </div>
</div>
<!--<h1 id="qunit-header"><a href="#">vie.js Speed Suite</a></h1>
View
@@ -406,3 +406,27 @@ test("Test RDFa datatype parsing", function () {
start();
});
});
+
+test("Test multiple RDFa instances of same entity", function () {
+ var z = new VIE();
+ z.use(new z.RdfaService);
+
+ var html = jQuery('#qunit-fixture .rdfa-twoinstances');
+
+ stop();
+ z.load({element: html}).from('rdfa').execute().done(function (entities) {
+ // Freshly parsed, there should be no recorded changes in the entity
+ var entity = z.entities.get('<http://example.net/example>');
+ equal(entity.changedAttributes(), false);
+ equal(entity.hasChanged('<http://purl.org/dc/terms/description>'), false);
+
+ // Change one value, now changes should be recorded
+ entity.set('dcterms:description', 'Baz');
+ equal(entity.get('<http://purl.org/dc/terms/description>'), 'Baz');
+ // Since the change has already been propagated to the view, we should
+ // have no changed attributes remaining
+ equal(entity.changedAttributes(), false);
+ equal(entity.hasChanged('<http://purl.org/dc/terms/description>'), false);
+ start();
+ });
+});

0 comments on commit 4e91550

Please sign in to comment.