locked
LS HTML - Proper way to clear a property from Edit screen RRS feed

  • Question

  • Hello all,

    What's the proper way to clear an entity property on an Edit screen without getting the "Unsaved Changes" warning?

    Imagine this hypothetical case:
    I have an entity called TestEntity, and it has two properties (Property1 [string] and Property2 [Integer] ). On the edit screen of this entity there's a databind to Property2, so each time Property2 is changed, the field with Property1 it's cleared (with the purpose that the user must change it).

    Something like this:

    myapp.AddEditTestEntity.Property1_postRender = function (element, contentItem) {
         contentItem.dataBind("screen.TestEntity.Property2",     function (newValue) {
    
                contentItem.value = undefined; //also tried null and ""
    
                
     });
    }
    It clears the field just has expected..

    Now imagine the user changes it's mind and clicks Cancel, if then he tries any navigation gets the messageBox "Unsaved Changes". I tried writing my own cancel button with cancelChanges() and screen.TestEntity.details.discardChanges() but it didn't worked.

    Any ideia? 
    Tuesday, January 5, 2016 6:27 PM

Answers

  • myapp.AddEditTestEntity.Property2_postRender = function (element, contentItem) {
        contentItem.dataBind("value", function (newValue) {
            var screen = contentItem.screen,
                oldValue = screen.TestEntity.Property2;
            setTimeout(function () {
                var entityState = screen.TestEntity.details.entityState;
                if (entityState !== msls.EntityState.unchanged && p2 !== newValue)
                    contentItem.screen.TestEntity.Property1 = null;
            }, 100);
        });
    };

    The change event will fire when initialising the screen/entity and also when cancelling the edit. That is why the dataBind handler needs the extra checks and the setTimeout to let the rest of the change code to complete.

    As tested in VS2015 / msls-2.5.3

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.


    • Edited by Xpert360 Wednesday, January 6, 2016 10:22 AM Tested
    • Marked as answer by Johny Pingo Wednesday, January 6, 2016 5:50 PM
    Wednesday, January 6, 2016 10:20 AM

All replies

  • contentItem.screen.TestEntity.Property1 = null;

    assuming it is optional/nullable...

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Tuesday, January 5, 2016 6:50 PM
  • Thanks for your reply Dave.

    I tried your solution, but get the same message: "Unsaved Changes".

    I think next i'll try using a local property as an input for Property1, and make my own Save method where i compare the local property with the original value of Property1, a in that moment change it or not.

    For this simple case i think it will work, but i'm not certain that it works on my real problem, where i have an Entity that as a set of another entity (ex. a Person with Name, BirthDate, Contacts, where Contact is an Entity and can be edited or added a new one inside the Person Edit screen).
    Wednesday, January 6, 2016 9:25 AM
  • There should be no need to do anything more complicated. On taking a closer look at your example there are other bits not quite right. Hang on a few moments.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.



    • Edited by Xpert360 Wednesday, January 6, 2016 10:45 AM
    Wednesday, January 6, 2016 9:33 AM
  • myapp.AddEditTestEntity.Property2_postRender = function (element, contentItem) {
        contentItem.dataBind("value", function (newValue) {
            var screen = contentItem.screen,
                oldValue = screen.TestEntity.Property2;
            setTimeout(function () {
                var entityState = screen.TestEntity.details.entityState;
                if (entityState !== msls.EntityState.unchanged && p2 !== newValue)
                    contentItem.screen.TestEntity.Property1 = null;
            }, 100);
        });
    };

    The change event will fire when initialising the screen/entity and also when cancelling the edit. That is why the dataBind handler needs the extra checks and the setTimeout to let the rest of the change code to complete.

    As tested in VS2015 / msls-2.5.3

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.


    • Edited by Xpert360 Wednesday, January 6, 2016 10:22 AM Tested
    • Marked as answer by Johny Pingo Wednesday, January 6, 2016 5:50 PM
    Wednesday, January 6, 2016 10:20 AM
  • Thanks Dave, that really helped a lot.

    Best Regards
    Wednesday, January 6, 2016 5:50 PM