Skip to content

Commit 8f0a932

Browse files
committed
Add Instance.markAsDirty(propName)
1 parent 7e6e9ab commit 8f0a932

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/Instance.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,15 @@ function Instance(Model, opts) {
657657
enumerable: false,
658658
writable: true
659659
});
660+
Object.defineProperty(instance, "markAsDirty", {
661+
value: function (propName) {
662+
if (propName != undefined) {
663+
opts.changes.push(propName);
664+
}
665+
},
666+
enumerable: false,
667+
writable: true
668+
});
660669
Object.defineProperty(instance, "isInstance", {
661670
value: true,
662671
enumerable: false

test/integration/instance.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,28 @@ describe("Model instance", function() {
254254
});
255255
});
256256

257+
describe("#markAsDirty", function () {
258+
var person = null;
259+
260+
beforeEach(function (done) {
261+
Person.create({ name: 'John', age: 44, data: { a: 1 } }, function (err, p) {
262+
if (err) return cb(err);
263+
264+
person = p;
265+
done();
266+
});
267+
});
268+
269+
it("should mark individual properties as dirty", function () {
270+
should.equal(person.saved(), true);
271+
person.markAsDirty('name');
272+
should.equal(person.saved(), false);
273+
should.equal(person.__opts.changes.join(','), 'name');
274+
person.markAsDirty('data');
275+
should.equal(person.__opts.changes.join(','), 'name,data');
276+
});
277+
});
278+
257279
describe("#isShell", function () {
258280
it("should return true for shell models", function () {
259281
should.equal(Person(4).isShell(), true);

0 commit comments

Comments
 (0)