Skip to content

Commit

Permalink
add default value support to getPropertyValue:
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Aug 29, 2017
1 parent c744ef1 commit d719062
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ export class Base {
* Returns the property value by name
* @param name property name
*/
public getPropertyValue(name: string): any { return this.propertyHash[name]; }
public getPropertyValue(name: string, defaultValue: any = null): any {
var res = this.propertyHash[name];
if(Base.isValueEmpty(res) && defaultValue != null) return defaultValue;
return res;
}
/**
* set property value
* @param name property name
Expand Down
3 changes: 2 additions & 1 deletion tests/basetests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class BaseTester extends Base {
function(newItem){newItem.isNew = true;},
function(deletedItem){deletedItem.isDeleted = true;});
}
public get value1(): number { return this.getPropertyValue("value1"); }
public get value1(): number { return this.getPropertyValue("value1", 1); }
public set value1(val: number) { this.setPropertyValue("value1", val); }
public get items(): Array<any> { return this.getPropertyValue("items"); }
}
Expand All @@ -126,6 +126,7 @@ QUnit.test("Base simple propety value", function (assert) {
oldValue = options.oldValue;
newValue = options.newValue;
});
assert.equal(base.value1, 1, "Use the default value");
base.value1 = 5;
assert.equal(base.value1, 5, "It has been assign correctly");
assert.equal(counter, 1, "event called one time");
Expand Down

0 comments on commit d719062

Please sign in to comment.