Skip to content

Commit b1ff634

Browse files
authored
Merge pull request #859 from dresende/add_is_dirty
Add isDirty function to model instances
2 parents c286f39 + 88a21c3 commit b1ff634

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### v7.1.0
2+
- Add `isDirty` function to instances ([859](../../pull/859))
3+
14
### v7.0.0
25
- Update `async` package from v2 to v3 to resolve security vulnerabilities ([858](../../pull/858))
36
- Drop support for node < 6 (due to `async` package update)

lib/Instance.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,12 @@ function Instance(Model, opts) {
670670
get: function () { return opts.changes; },
671671
enumerable: false
672672
});
673+
Object.defineProperty(instance, "isDirty", {
674+
value: function () {
675+
return opts.changes.length > 0;
676+
},
677+
enumerable: false
678+
});
673679
Object.defineProperty(instance, "isInstance", {
674680
value: true,
675681
enumerable: false

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sqlite",
1313
"mongodb"
1414
],
15-
"version": "7.0.0",
15+
"version": "7.1.0",
1616
"license": "MIT",
1717
"homepage": "http://dresende.github.io/node-orm2",
1818
"repository": "http://github.com/dresende/node-orm2.git",

test/integration/instance.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,35 @@ describe("Model instance", function() {
303303
});
304304
});
305305

306+
describe("#isDirty", function () {
307+
var person = null;
308+
309+
beforeEach(function (done) {
310+
Person.create({ name: 'John', age: 44, data: { a: 1 } }, function (err, p) {
311+
if (err) return cb(err);
312+
313+
person = p;
314+
done();
315+
});
316+
});
317+
318+
it("should return false by default", function () {
319+
should.equal(person.isDirty(), false);
320+
});
321+
322+
it("should return false when property is set to same value", function () {
323+
should.equal(person.isDirty(), false);
324+
person.name = 'John';
325+
should.equal(person.isDirty(), false);
326+
});
327+
328+
it("should return true when property is changed", function () {
329+
should.equal(person.isDirty(), false);
330+
person.name = 'Bob';
331+
should.equal(person.isDirty(), true);
332+
});
333+
});
334+
306335
describe("#isShell", function () {
307336
it("should return true for shell models", function () {
308337
should.equal(Person(4).isShell(), true);

0 commit comments

Comments
 (0)