Question

  vm.alterExistingSortOrder = function () {

        $.each(vm.M1010DxCodeSorterListBox2(), function (index1, value1) {


            if (vm.M1010DxCodeSorterListBox2()[index1].IsNewDxCode == false)
            {
                 return datacontext.fetchEntity("EpisodeDxM00Rel", vm.M1010DxCodeSorterListBox2()[index1].Id).then(function (result) {
                    var episodeDxM00RelEntity = result.entity;
                    episodeDxM00RelEntity.SortOrder(index1);
                    vm.save();
                });

            }

        });


        $.each(vm.M1016DxCodeSorterListBox2(), function (index2, value2) {


            if (vm.M1016DxCodeSorterListBox2()[index2].IsNewDxCode == false)
            {
                 return datacontext.fetchEntity("EpisodeDxM00Rel", vm.M1016DxCodeSorterListBox2()[index2].Id).then(function (result) {
                    var episodeDxM00RelEntity = result.entity;
                    episodeDxM00RelEntity.SortOrder(index2);
                    vm.save();
                });

            }

        });
};

I have a function in which I have 8 for loops (in code sample I am just showing 2 loops). I am fetching an entity within that loop, and changing one of its properties. Ideally I want to save all the entities together instead of saving one by one which I am currently doing and that gives me the concurrent save error but I am not sure how can I save all these modified entities together (where should I write a single save method call considering there are multiple promises being returned by the fetchEntity methods)

Was it helpful?

Solution

Breeze does support concurrent saves but you need to explicitly turn this capability on via the SaveOptions class: http://www.breezejs.com/sites/all/apidocs/classes/SaveOptions.html

myEntityManager.saveOptions = new SaveOptions({ allowConcurrentSaves: true });
myEntityManager.saveChanges().then(...);

That said, it probably makes more sense to refactor/rewrite your code so that you can perform a single save with multiple entities. Take a look at the Q.all method (https://github.com/kriskowal/q) to see how to compose a save promise that is performed after ALL of your fetches have been performed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top