The DataContext class: cancelling changes

In the previous post, we’ve see how to execute some basic actions over a DataContext instance. In this post we’ll keep looking at the it and we’ll see the available options for working with changes made over the data tracked by the control. Before getting started, we’ll assume that _ctx references an existing DataContext object created previously with the code shown in the previous post.

The first thing you can do is cancel all the changes that have been made to the data encapsulated by the control. You can easily do this by calling the clearChanges method. This method ends up doing several things:

  • it starts by cleaning the internal arrays used for keeping track of the items that were inserted, edited and removed (ie, it sets the _inserts_edits and _deletes objects to null);
  • it clears the private _changelist array (by using the Sys.Observer.clear method);
  • it sets the _hasChanges property to false and generates the propertyChanged event.

Here’s some demo code that shows how to use this method:

var aux = { name: "charles", address: "lx" };
Sys.Observer.setValue(aux, "address", "pt");
alert(_ctx.get_changes().length); //at least 1 change here!
alert(_ctx.get_changes().length);//no changes here

As you can see, inserting a new entity ensures that the _changelist is not empty. After calling the clearChanges method, all pending changes are deleted. Even though pending changes are cancelled (ie, cleared), you still have access to the initial data that was tracked by the control (that is,if the control encapsulated any data that was being tracked).

If you also need to clear the tracked data,then you’ll need to resort to the clearData method. Besides clearing pending changes, this method will also clear the internal arrays it uses (including _items and _lastResults) ensuring proper clean up of the internal state of the control. Here’s some demo code that shows how to use this method:

alert(_ctx.get_lastFetchDataResults() === null);//true!

As you can see, calling the clearData method means that you’ll end up “nulling” the private _getchanges field of the DataContext instance.

Besides cancelling changes, you can also “commit” changes. But before we go into this topic, we need to talk about interacting with remote web services. And that’s what we’ll start doing in the next post. Stay tuned for more.


~ by Luis Abreu on November 2, 2009.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: