0

I've added the changeTracker here to my ko application but can't get it to initialize after all my bindings have updated. I'm doing several ajax calls to load data from a sql server database, and call

viewModel.tracker().markCurrentStateAsClean();

after updating everything from the database but apparently ko is still busy loading data after attempting to reset/initial the tracking mechanism. That is, the somethingHasChanged function is still true even after resetting it. I've tentatively added

setTimeout(function () { viewModel.tracker().markCurrentStateAsClean(); }, 4000);

and this works, but it just seems like it could cause problems with slow networks or low bandwidth situations where it takes longer to load. You can refer to my previous SO question here. Is there a better way to do this?

UPDATE

My database calls use

$.when(fcn1(), fcn2(), fcn3(), fcn4())
.done(function msg1, msg2, msg3, msg4) {
    // initialize ko objects, "assistants", "teachers", "assistants" not assigned, etc.
}) 
.fail(function blah blah blah) {});

and so I've tried adding

viewModel.tracker().markCurrentStateAsClean();

at the end of the doneCallback and even to the end of jQuery/javascript fcns that call those view model functions to no avail - the somethingHasChanged() remains at true even when I try to reset it. If I wait for 4 seconds with the setTimeout call, clearly after ko has finished updating/binding, the somethingHasChanged() method returns false. So between 0 and 4 seconds after I'd think it's finished, it's really finished, when somethingHasChanged() is false.

And my view model has a lot of extenders due to race condition dependencies that are getting set up initially so I don't want the observables/observable arrays to notify subscribers but just once at the end of all updates, like this:

self.teachers = ko.observableArray().extend({ rateLimit: 0 });
self.columns = ko.observableArray().extend({ rateLimit: 0 });
self.assistants = ko.observableArray().extend({ rateLimit: 0 });

HTH in the explanation.

Community
  • 1
  • 1
  • Can you post more code to show the area where the ajax call completes and populates the model along with where `markCurrentStateAsClean` is called in respect to the ajax. – James Thorpe Dec 22 '14 at 14:45
  • This is a pretty large application with around 1200 + lines of view model code. There's probably ways to reduce that, but let's just say it's complex with lots of moving parts. Basically it has two modes - creating a new "entry" and editing an existing "entry". The entries are three grids of something like teachers and assistant teachers with a final grid containing "assistants" from a given division/department/group that were not assigned/associated with the given selected teachers. It loads available "teachers" or "assistants" and after it's done, I call markCurrentStateAsClean(). – sfors says reinstate Monica Dec 22 '14 at 20:29
  • It's a different strategy, but I see this SO post - http://stackoverflow.com/questions/12422708/knockoutjs-track-changes-after-ajax-call - that talks about John Papa's KOLite and PubSub mechanism that looks interesting. – sfors says reinstate Monica Dec 22 '14 at 20:45

0 Answers0