Skip to content

Commit

Permalink
feat(Object): add #dirtyKeys
Browse files Browse the repository at this point in the history
remove #hasChanged and #changedAttributes in favor of #dirty and #dirtyKeys
  • Loading branch information
leeyeh committed Dec 10, 2019
1 parent 3fa5826 commit 847ba23
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 40 deletions.
46 changes: 10 additions & 36 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1227,42 +1237,6 @@ module.exports = function(AV) {
return this;
},

/**
* Determine if the model has changed since the last <code>"change"</code>
* 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
* <code>"change"</code> event was fired.
Expand Down
3 changes: 1 addition & 2 deletions storage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<this>;
dirty(attr: String): boolean;
dirtyKeys(): string[];
escape(attr: string): string;
fetch(fetchOptions?: FetchOptions, options?: AuthOptions): Promise<this>;
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;
Expand Down
4 changes: 2 additions & 2 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down

0 comments on commit 847ba23

Please sign in to comment.