none
Lightswitch HTML - afterApplyChanges event

    Question

  • Hi,

    I need to call a function to refresh data after the data on an Edit screen is updated (i.e., I need a function "afterApplyChanges"). I know that Lightswitch HTML has a beforeApplyChanges function. How can I implement a function "afterApplyChanges"?

    Thanks!

    Wednesday, July 24, 2013 8:10 PM

Answers

  • I found a solution by hacking the code in /Scripts/datajs-1.1.0.min. There is a function called "stt" (stt = function (n, t, i)) at line 14 column 67381. That function is called AFTER the changes are saved to the database. I defined a custom function "myapp.ChangesSaved" and in the function "stt" I call that function. 

    Not the cleanest solution. However, it gets me what I need for the current time. I'm certainly open to other solutions.

    • Marked as answer by Abdulla16 Thursday, July 25, 2013 10:01 PM
    Thursday, July 25, 2013 10:01 PM

All replies

  • Oh, so close....but the LS team went with myapp.onsavechanges instead of afterApplyChanges after a long and protracted battle about what to call this function.

    See Customize the Save command to save to multiple data sources for a usage example.


    Wednesday, July 24, 2013 8:49 PM
  • Thanks for your reply!

    But it looks like onsavechanges is called BEFORE saving the data to the database. I need a function that will get called AFTER the data is saved to the database. 

    Thursday, July 25, 2013 4:59 AM
  • I found a solution by hacking the code in /Scripts/datajs-1.1.0.min. There is a function called "stt" (stt = function (n, t, i)) at line 14 column 67381. That function is called AFTER the changes are saved to the database. I defined a custom function "myapp.ChangesSaved" and in the function "stt" I call that function. 

    Not the cleanest solution. However, it gets me what I need for the current time. I'm certainly open to other solutions.

    • Marked as answer by Abdulla16 Thursday, July 25, 2013 10:01 PM
    Thursday, July 25, 2013 10:01 PM
  • That approach sounds highly suspect and is not recommended.

    You didn't give much detail of what you were trying to achieve, so offering other solutions is also sketchy.  If you had wanted to trigger a function after an item is saved in the database, you have all of the existing server procedures in the save pipeline (Entity_Inserted, Entity_Updated, SaveChanges_Executed) as well as the JavaScript and LightSwitch API methods .addEventListener("collectionchange", listener), or .addChangeListener() to trigger a function when data is saved.

    Friday, July 26, 2013 4:39 AM
  • .addEventListener("collectionchange", listener), or .addChangeListener() didn't work for me as they already get fired on changing the fieldvalue itself and not after saving it to the database. Or do we have to add the event listener to another collection as the screen collection (screen.SomeCollectionItems)?

    I think he tries the same as me to get some computed value from the server, which doesn't get updated on client side after saving. The problem is on a second save of the entity the client checks the current values of the database before saving it and as they are different to the chached one of the client it throws a transaction exeption as it thinks the data has changed from another person since last saving.

    So the solution was to reload the collection/entity to get the current values after saving it.

    Friday, July 26, 2013 10:53 AM
  • Please see:

    New API For Refreshing Data in LightSwitch in Visual Studio 2013

    The new refresh() API consists of  two methods that each return a Promise object.

    • refresh()      
      • Asynchronously loads the first page of items into this collection and           
        returns a promise that will be fulfilled when the first page is loaded.            
        Existing results will be refreshed on the first page and subsequent            
        pages unless load() is called again.
    • refresh(navigationPropertyNames)      
      • Updates the entity with values from the data source if the entity           
        is not changed.
                  
        • <param name="navigationPropertyNames" type="Array" optional="true">                
          An array of names of navigation properties to be included. An empty                
          array means no properties will be included. If not specified, all                
          reference properties are included.

    Sample usage

    • Refresh the Order entity and its Customer, Employee, Shipper      
      • screen.Order.details.refresh();
    • Refresh only the Order entity      
      • screen.Order.details.refresh([]);
    • Refresh the Order entity and its Customer      
      • screen.Order.details.refresh(["Customer"]);

    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com

    Friday, July 26, 2013 1:08 PM
  • There is also the

    msls.NavigateBackAction.commit || cancel

    property for determining save versus cancel from a recently closed screen.

    Friday, July 26, 2013 2:37 PM
  • Hi Abdulla16,

    You can implement BeforeSave and AfterSave like this:

    myapp.onsavechanges = function (e) {

        // BEFORE SAVE

        var ent = myapp.activeDataWorkspace.ApplicationData.Table1Items.addNew()

        ent.s = "added before, will be saved";

        e.detail.promise = myapp.activeDataWorkspace.ApplicationData.saveChanges().then(function (e) {

            // AFTER SAVE

            var ent2 = myapp.activeDataWorkspace.ApplicationData.Table1Items.addNew();

            ent2.s = "added after, won't be saved";

        });

    Regards,

    Burt

    Wednesday, November 20, 2013 11:51 PM