none
Show AddEdit dialog with initialized parameters in HTML Client app RRS feed

  • Question

  • Hi, I'm starting building an HTML Application with LightSwitch.

    I'm trying to implement a "copy item" function: I select an item, press "copy" button and then I want the same "Add New" dialog to be showed with a new item with pre-compiled fields derived from the copied one.

    I implemented this:

    myapp.BrowseItineraryTypes.copyItineraryType_execute = function (screen) {
        // Write code here.
        var copied_item = screen.ItineraryTypes.selectedItem;
        var new_item = screen.ItineraryTypes.addNew();
     
        new_item.bActive = copied_item.bActive;
        new_item.sDescription = copied_item.sDescription;
     
        myapp.showAddEditItineraryType(new_item);
    };

    But I'm getting an "unsaved changes" alert telling me that "There are unsaved changes on this page." 

    I'm guessing that's why I've created an item ("screen.ItineraryTypes.addNew()") before opening the AddEdit dialog. 

    How can I get the required behaviour ?

    Thanks in advance.

    Paolo. 

    Monday, April 22, 2013 3:53 PM

Answers

  • Hi Paolo,

    Here's a similar thread, with a sample app on it.

    The short answer is you want to leverage the beforeShown callback to create the new entity and set any custom code.

    myapp.BrowseItineraryTypes.copyItineraryType_execute = function (screen) {
        myapp.showAddEditItineraryType(null, {
            beforeShown: function(addNewScreen) {
                var copied_item = screen.ItineraryTypes.selectedItem;
                var new_item = screen.ItineraryTypes.addNew();
     
                new_item.bActive = copied_item.bActive;
                new_item.sDescription = copied_item.sDescription;
       
                // Note: Assuming InineraryType is the
                // property name on AddEditItineraryType.
                addNewScreen.InineraryType = new_item;
            }
        });
    };

    Best regards,
    Huy Nguyen

    • Marked as answer by Mappao Monday, April 22, 2013 4:23 PM
    Monday, April 22, 2013 4:18 PM

All replies

  • Hi Paolo,

    Here's a similar thread, with a sample app on it.

    The short answer is you want to leverage the beforeShown callback to create the new entity and set any custom code.

    myapp.BrowseItineraryTypes.copyItineraryType_execute = function (screen) {
        myapp.showAddEditItineraryType(null, {
            beforeShown: function(addNewScreen) {
                var copied_item = screen.ItineraryTypes.selectedItem;
                var new_item = screen.ItineraryTypes.addNew();
     
                new_item.bActive = copied_item.bActive;
                new_item.sDescription = copied_item.sDescription;
       
                // Note: Assuming InineraryType is the
                // property name on AddEditItineraryType.
                addNewScreen.InineraryType = new_item;
            }
        });
    };

    Best regards,
    Huy Nguyen

    • Marked as answer by Mappao Monday, April 22, 2013 4:23 PM
    Monday, April 22, 2013 4:18 PM
  • Hi Huy,

    this also works as expected.

    Many thanks indeed again, I'd like to avoid simply (or silly ? ;)) questions like those but HTML Client infos aren't yet so much as non-HTML apps.

    Ciao.

    Paolo.

    Monday, April 22, 2013 4:26 PM
  • Hi Huy,

    is there a way to access control's properties in "addNewScreen" ? For example, is there a way to get the selected item of a list in the child screen ?

    I tried implementing code in the "afterClosed" function with no success...

    Thanks in advance

    Paolo.

    Tuesday, April 30, 2013 4:25 PM
  • Hi Paolo,

    Instead of trying to access the controls (the Views), you should access the screen's properties (the View Models). Most of the time a List View in a screen is data-bound to a Visual Collection (exposed as the value of a screen's Collection Property), so the selected item on a List View can be found on the Visual Collection as well.

    2013/05/02 Edit: However, in the afterClosed call back, screen.Customers will be undefined because the object is disposed. See my post below.

    Best regards,
    Huy


    Tuesday, April 30, 2013 4:48 PM
  • Hi Huy,

    I tried the same way but "addEditScreen.Customers" is undefined. More specifically, everything (properties, objects, etc) are named but undefined...

    What I'm trying to do is to open a browsing screen to choose an item, just like a details modal picker...

    Thursday, May 2, 2013 8:35 AM
  • Hi Paolo,

    Sorry, this was my mistake. I just realized yesterday working on the same sample app that by the time afterClosed is invoked, most complex objects on the screen (like Visual Collections) are disposed.

    You have two choices (let's say you're trying to set an Order Customer).

    1. Pass the Order to the Browse Screen and set the Order.Customer in that screen. You can see the details and my sample app in this post.
    2. On the Browse Screen, add a local property of type Customer (let's say SelectedCustomer) and whenever selected item change, assign the value to this local property. Then in afterClosed SelectedCustomer should still be there.

    Best regards,
    Huy Nguyen


    Thursday, May 2, 2013 4:07 PM
  • Choice n. 2, well done... ;)

    Ciao.

    Tuesday, May 28, 2013 12:01 PM