I would like to know which property in the JSON model has changed when modified by a view. For a test I took OpenUI5 walkthrough example and added the following lines in the application controller
oProductModel.attachPropertyChange( function(oEvent){
console.log("event: ", oEvent);
}, this);
When I change a property in the text input, the function in the attachPropertyChange is called but oEvent object is empty as I print it in console.
I know I could connect to text input change event, but I would like to use attachPropertyChange in case there would be multiple views of the same model.
As far as I understood, you'd like to avoid using the
change
event of the Input control because there is no information about which property in the model has changed. However, you can still get all those information within the change handler via:event.getSource().getBinding(/*controlPropertyName*/).getPath()
to get the name of the bound property, orevent.getSource().getBindingContext(/*modelName*/).getPath(/*suffix*/)
to get the path of the bound context. ThegetPath
here awaits an optional suffix that will be appended to the context path with a"/"
in between.You can combine those two APIs to get an absolute path in case the property binding was relative. E.g.:
Demo
Note: Currently, the API
getPath
of Binding is not visible in the API Reference due to the missing@public
annotation. However, that's an issue of UI5 documentation which will be fixed soon or later. Until then, the source code can be found here.You can use change event for all input field in UI, and write event handling method in the controller. You will get the property as well as value in the oEvent of the event handling method easily. I hope you understood.