From 847ba239744b48754d30a597540d6d346ecc7bdc Mon Sep 17 00:00:00 2001 From: Lee Yeh Date: Tue, 10 Dec 2019 16:20:16 +0800 Subject: [PATCH] feat(Object): add #dirtyKeys remove #hasChanged and #changedAttributes in favor of #dirty and #dirtyKeys --- src/object.js | 46 ++++++++++------------------------------------ storage.d.ts | 3 +-- test/object.js | 4 ++-- 3 files changed, 13 insertions(+), 40 deletions(-) diff --git a/src/object.js b/src/object.js index 2a27181a5..f9b667579 100644 --- a/src/object.js +++ b/src/object.js @@ -334,6 +334,16 @@ module.exports = function(AV) { return false; }, + /** + * Returns the keys of the modified attribute since its last save/refresh. + * @return {String[]} + */ + dirtyKeys: function() { + this._refreshCache(); + var currentChanges = _.last(this._opSetQueue); + return _.keys(currentChanges); + }, + /** * Gets a Pointer referencing this Object. * @private @@ -1227,42 +1237,6 @@ module.exports = function(AV) { return this; }, - /** - * Determine if the model has changed since the last "change" - * event. If you specify an attribute name, determine if that attribute - * has changed. - * @param {String} attr Optional attribute name - * @return {Boolean} - */ - hasChanged: function(attr) { - if (!arguments.length) { - return !_.isEmpty(this.changed); - } - return this.changed && _.has(this.changed, attr); - }, - - /** - * Returns an object containing all the attributes that have changed, or - * false if there are no changed attributes. Useful for determining what - * parts of a view need to be updated and/or what attributes need to be - * persisted to the server. Unset attributes will be set to undefined. - * You can also pass an attributes object to diff against the model, - * determining if there *would be* a change. - */ - changedAttributes: function(diff) { - if (!diff) { - return this.hasChanged() ? _.clone(this.changed) : false; - } - var changed = {}; - var old = this._previousAttributes; - AV._objectEach(diff, function(diffVal, attr) { - if (!_.isEqual(old[attr], diffVal)) { - changed[attr] = diffVal; - } - }); - return changed; - }, - /** * Gets the previous value of an attribute, recorded at the time the last * "change" event was fired. diff --git a/storage.d.ts b/storage.d.ts index c7cbaafc6..e3b6d757b 100644 --- a/storage.d.ts +++ b/storage.d.ts @@ -322,19 +322,18 @@ export class Object extends BaseObject { bitOr(attributeName: string, item: number): this; bitXor(attributeName: string, item: number): this; change(options: any): this; - changedAttributes(diff: any): boolean; clear(options: any): any; revert(keys?: string | string[]): this; clone(): this; destroy(options?: Object.DestroyOptions): Promise; dirty(attr: String): boolean; + dirtyKeys(): string[]; escape(attr: string): string; fetch(fetchOptions?: FetchOptions, options?: AuthOptions): Promise; fetchWhenSave(enable: boolean): void; get(attr: string): any; getACL(): ACL; has(attr: string): boolean; - hasChanged(attr: string): boolean; increment(attr: string, amount?: number): this; isValid(): boolean; op(attr: string): any; diff --git a/test/object.js b/test/object.js index 876371f63..096ff6307 100644 --- a/test/object.js +++ b/test/object.js @@ -127,9 +127,7 @@ describe('Objects', function() { it('stringify and parse', () => { const text = AV.stringify(gameScore); - console.log(text); const parsedGameScore = AV.parse(text); - console.log(parsedGameScore); parsedGameScore.should.be.instanceof(GameScore); parsedGameScore.id.should.eql(gameScore.id); parsedGameScore.get('id').should.eql(gameScore.get('id')); @@ -368,6 +366,7 @@ describe('Objects', function() { }); object.revert(); object.dirty().should.eql(false); + object.dirtyKeys().should.eql([]); object.get('name').should.eql('AVOSCloud'); object.get('age').should.eql(0); object.toFullJSON().should.eql(data); @@ -380,6 +379,7 @@ describe('Objects', function() { }); object.revert('name'); object.dirty().should.eql(true); + object.dirtyKeys().should.eql(['age']); object.get('name').should.eql('AVOSCloud'); object.get('age').should.eql(1); });