Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

observe callback function second parameter 'oldValue' is always undefined #14

Open
erotavlas opened this issue Apr 20, 2017 · 1 comment

Comments

@erotavlas
Copy link
Contributor

erotavlas commented Apr 20, 2017

The listener that observes for changes to property objects, contains the newValue but the oldValue is always undefined.

This documentation states that "The listener will be called with the the new value as the first argument, and the old value as the second argument."

So in the example below i expect when put(7) is called that in the callback function the newValue is 7 and the oldValue is 5 - however oldValue is undefined.

      var validatingMemory = (declare([Memory, Validating]))({
            Model: jsonSchema({
                "type": "object",
                "required": ["name"],
                "properties": {
                    "prime": {
                        "type": 'boolean'
                    },
                    "number": {
                        "type": 'number',
                        "minimum": 1,
                        "maximum": 10
                    },
                    "name": {
                        "type": 'string'
                    },
                    "mappedTo": {
                        "type": 'string'
                    }
                },
                "additionalProperties": false
            }),

            idProperty: "name"
        });  

      var data = [
            { name: 'one', number: 1, prime: false, mappedTo: 'E' },
            { name: 'two', number: 2, prime: true, mappedTo: 'D' },
            { name: 'three', number: 3, prime: true, mappedTo: 'C' },
            { name: 'four', number: 4, even: true, prime: false, mappedTo: 'B' },
            { name: 'five', number: 5, prime: true, mappedTo: 'A' }
        ];
        //convert data to json string
        var jsonString = JSON.stringify(data);

        //convert string back to json object and insert back to Model
        var obj = JSON.parse(jsonString);
        validatingMemory.setData(obj);

        var testingProperty = validatingMemory.getSync("five");
        var testingPropertyObject = testingProperty.property('number');

        testingPropertyObject.observe(
            function (newValue, oldValue) {
                console.log("old value: " + oldValue);
                console.log("changed to new value: " + newValue);
            },
            {
                onlyFutureUpdates: false
            }
        );
        testingPropertyObject.put(7);
@erotavlas erotavlas changed the title observe callback function parameter for 'oldValue' is always undefined observe callback function second parameter for 'oldValue' is always undefined Apr 20, 2017
@erotavlas erotavlas changed the title observe callback function second parameter for 'oldValue' is always undefined observe callback function second parameter 'oldValue' is always undefined Apr 20, 2017
@erotavlas
Copy link
Contributor Author

erotavlas commented Apr 20, 2017

I made a change to Model.js which I think works. In the Reactive object I change the observe property as follows (in var handle = this._addlistener() I added the second parameter oldValue )

	observe: function (/*function*/ listener, /*object*/ options) {
		//	summary:
		//		Registers a listener for any changes in the current value
		//	listener:
		//		Function to be called for each change
		//	options.onlyFutureUpdates
		//		If this is true, it won't call the listener for the current value,
		//		just future updates. If this is true, it also won't return
		//		a new reactive object
		
		var reactive;
		if (typeof listener === 'string') {
			// a property key was provided, use the Model's method
			return this.inherited(arguments);
		}
		if (!options || !options.onlyFutureUpdates) {
			// create a new reactive to contain the results of the execution
			// of the provided function
			reactive = new Reactive();
			if (this._has()) {
				// we need to notify of the value of the present (as well as future)
				reactive.value = listener(this.valueOf());
			}
		}
		// add to the listeners
		var handle = this._addListener(function (value, oldValue) {
			var result = listener(value, oldValue);
			if (reactive) {
				// TODO: once we have a real notification API again, call that, instead 
				// of requesting a change
				reactive.put(result);
			}
		});
		if (reactive) {
			reactive.remove = handle.remove;
			return reactive;
		} else {
			return handle;
		}
	},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant