Resolved #322: Creating an Observable by passing a JSON object in the constructor does not define the respective properties on the Observable object instance.

This commit is contained in:
Rossen Hristov
2015-06-15 13:17:58 +03:00
parent 68355e947e
commit f41d7b83a1
2 changed files with 63 additions and 0 deletions

View File

@ -8,13 +8,32 @@ interface ListenerEntry {
export class Observable implements definition.Observable {
public static propertyChangeEvent = "propertyChange";
private _map: Map<string, Object>;
private _observers = {};
constructor(json?: any) {
if (json) {
this._map = new Map<string, Object>();
var that = this;
var definePropertyFunc = function definePropertyFunc(propertyName) {
Object.defineProperty(Observable.prototype, propertyName, {
get: function () {
return that._map.get(propertyName);
},
set: function (value) {
that._map.set(propertyName, value);
that.notify(that._createPropertyChangeData(propertyName, value));
},
enumerable: true,
configurable: true
});
};
for (var prop in json) {
if (json.hasOwnProperty(prop)) {
definePropertyFunc(prop);
this.set(prop, json[prop]);
}
}