Skip to content

Commit 98d2109

Browse files
committed
Make Stateful call custom setters/getters for function properties too.
Also changed the naming of the shadow properties to avoid a conflict where the getTextDir() property got a shadow property called _getTextDirAttr(), which looked like a custom getter for the textdir property. This shouldn't affect existing code assuming it uses this.get("prop") rather than using the name of the shadow property directly. Fixes #52 and refs ibm-js/delite#314.
1 parent 3f918c5 commit 98d2109

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

Stateful.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ define([
1717
var uc = name.replace(/^[a-z]|-[a-zA-Z]/g, function (c) {
1818
return c.charAt(c.length - 1).toUpperCase();
1919
});
20-
var ret = apn[name] = {
21-
p: "_" + name + "Attr", // shadow property, since real property hidden by setter/getter
20+
return apn[name] = {
21+
p: "_shadow" + uc + "Attr", // shadow property, since real property hidden by setter/getter
2222
s: "_set" + uc + "Attr", // converts dashes to camel case, ex: accept-charset --> _setAcceptCharsetAttr
2323
g: "_get" + uc + "Attr"
2424
};
25-
return ret;
2625
}
2726

2827
/**
@@ -38,7 +37,7 @@ define([
3837
});
3938
}
4039

41-
var REGEXP_SHADOW_PROPS = /^_(.+)Attr$/;
40+
var REGEXP_IGNORE_PROPS = /^constructor$|^_set$|^_get$|^deliver$|^discardChanges$|^_(.+)Attr$/;
4241

4342
/**
4443
* Base class for objects that provide named properties with optional getter/setter
@@ -82,7 +81,7 @@ define([
8281
getProps: function () {
8382
var hash = {};
8483
for (var prop in this) {
85-
if (typeof this[prop] !== "function" && !REGEXP_SHADOW_PROPS.test(prop)) {
84+
if (!REGEXP_IGNORE_PROPS.test(prop)) {
8685
hash[prop] = true;
8786
}
8887
}

tests/unit/Stateful.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ define([
1313
baz: "",
1414

1515
_getFooAttr: function () {
16-
return this._fooAttr - 1;
16+
return this._get("foo") - 1;
1717
},
1818

1919
_setBarAttr: function (value) {
@@ -391,6 +391,7 @@ define([
391391
// Check to make sure reported changes are consistent between platforms with and without Object.observe()
392392
// native support
393393
var dfd = this.async(1000),
394+
nop = function () {},
394395
stateful = new (dcl(Stateful, {
395396
_private: 1,
396397

@@ -403,12 +404,13 @@ define([
403404
this.instanceProp = 3;
404405
},
405406

406-
anotherFunc: function () { }
407+
anotherFunc: nop
407408
}))({});
408409
stateful.observe(dfd.callback(function (oldValues) {
409410
assert.deepEqual(oldValues, {
410411
_private: 1,
411-
foo: 2
412+
foo: 2,
413+
anotherFunc: nop
412414
});
413415
}));
414416
stateful._private = 11;

0 commit comments

Comments
 (0)