locked
HTML Client - Ensure data saving on button click RRS feed

  • Question

  • Hi all,

    currently I'm facing some problems with LightSwitch HTML client.

    It seems, that myapp.applyChanges() (or commitChanges()) are the only methods you can call on the HTML client to save your changes. However, as the function definitions state, this only merges your change set into a "parent change set" which may are may not always results in a persistent save to the database.

    So, what I want to achieve is, that on a button press all pending changes will be written to the database (and not only maybe, and maybe just added to the parent change set). Is there a way to do that via code?

    Best regards

    Simon


    • Edited by smnpl Friday, May 8, 2015 1:28 PM
    Friday, May 8, 2015 1:28 PM

Answers

  • It depends on how your screen is set up and the databinding between the entities.  For example, if you have a Northwinds setup, and you edit Products then tab over to Categories and edit Categories, your changes to Categories will be nested in with your Product changes.  If you view products > tab to Categories then edit, your changes to Categories will not be nested.  Therefore, you need to ensure you are not binding through the navigation property (e.g. Product.Category) if you don't want those changes to be nested.  Hope this makes sense.
    • Marked as answer by Angie Xu Monday, May 18, 2015 1:33 AM
    Tuesday, May 12, 2015 2:25 AM

All replies

  • Simon: Yes, you should be able to do this.  If the multiple change sets are across different data sources (like, part goes to ApplicationData, and part goes to another database), then you have to join the async save promises together so that the Save operation doesn't return until all data source changes return.  There is an example of how to do this at https://msdn.microsoft.com/en-us/library/jj733572.aspx#save

    myapp.onsavechanges = function (e) {
    
        var promises = [];
    
        promises.push(myapp.activeDataWorkspace.NorthwindData.saveChanges());
    
        promises.push(myapp.activeDataWorkspace.ApplicationData.saveChanges());
    
        e.detail.promise = WinJS.Promise.join(promises);
    
    };

    Another thing you could try is to just put a second call into myapp.applyChanges into a .then() or .done() block.  Something like:

    myapp.applyChanges().then(function(){
       myapp.applyChanges();
    });

    I haven't tried that out yet, so it's just a wild guess.  If the first one writes the child changes successfully, then the second one should write the parent changes and complete.  If you have more than two levels of hierarchical data, or parent->children->grandchildren...etc., scenarios, it may take even more nested Apply calls, but that's kind of an inelegant solution.

    Hope this helps a bit,

    R.T. Watkins

    Monday, May 11, 2015 6:31 PM
  • It depends on how your screen is set up and the databinding between the entities.  For example, if you have a Northwinds setup, and you edit Products then tab over to Categories and edit Categories, your changes to Categories will be nested in with your Product changes.  If you view products > tab to Categories then edit, your changes to Categories will not be nested.  Therefore, you need to ensure you are not binding through the navigation property (e.g. Product.Category) if you don't want those changes to be nested.  Hope this makes sense.
    • Marked as answer by Angie Xu Monday, May 18, 2015 1:33 AM
    Tuesday, May 12, 2015 2:25 AM