The easiest way of implementing updates would have been to bind the details view to the master view. This technique is used in an excellent video tutorial on Channel 9. I decided to adopt a different method for three reasons, two of which I will mention here. I wanted the user’s changes to be submitted by clicking the OK button in the popup window. With data binding in place, however, the master view, which is visible in the background, would update whenever the user tabbed away from a field in the details view. The user could be confused about whether his changes had already been submitted. If the user clicked cancel in the popup window, I of course wanted any changes he had made to be forgotten; and yet I could not find an automatic way to undo the changes. For these reasons, together with a third, more important one, I chose to perform the update manually. As it turned out, this too was straightforward. I added a handler to the OK button of my popup window by setting its OnOkScript parameter:
In the application init handler, I also created AdoNetDataContext component with my Mix Data Service as it source. I assigned this component to a global variable dataContext, as I wanted it to be available throughout my application.
In my webpage, I changed the dataprovider of my DataView to this new dataContext:
The function handling the OK button is simple:
I use the get_selectedIndex method of the Presenters DataView to find which item is being edited. I then use the get_items method to access the array of items. To inform the DataView of the changes being made to its data, it is necessary to use the Sys.Observer.setValue method. This method takes three parameters: first, the object to be changed; second, the name of the property to be changed; and third, the new value of that property. The setValue method changes the data in memory. To store the new data in the database, the saveChanges method of our dataContext is used. This method can be called without any parameters, but here I have added two callbacks, and set the third parameter, the context, to the name of the operation being performed.