locked
Adding a new record with JavaSript RRS feed

  • Question

  • Hi

    I'm still struggling with JavaScript and get lost quite often. A case in point is illustrated below.

    What I am trying to do here is if an "Acton" record already exists, allow the user to modify the action.

    If there is no record, add a record and allow the user to edit it.

    Seems simple and in C# I could do this easily but for some reason I haven't got my head around JavaScript yet and I'm just wasting a lot of time trying to figure it out :-/

    Your assistance will be greatly appreciated.

    Thanks, Mark.


    Mark

    Tuesday, July 9, 2013 3:08 AM

Answers

  • Hi Mark,

    The sample code already contains the event handler - $(window).one("popupafterclose", function(){ . That handler will fire once after the current pop up is closed and all the existing changes will be saved. Keep in mind when you call saveChanges all changes will be persisted, not just the newly added or edited Action Item. So it may be safer to let the user explicitly saving the changes using Save / Cancel.

    Best regards,
    Huy

    • Marked as answer by ITPSB Friday, July 12, 2013 2:04 AM
    Thursday, July 11, 2013 3:17 PM

All replies

  • Hi Mark,

    Your screenshot is reduced so it's not very clear what you're trying to achieve. Let's say this is your scenario:

    • Screen has a property named Action.
    • Screen also has a popup named ActionPopup already data-bound to Action.
    • Button handler should do what you want above.

    Then the code can be

        if (!screen.Action) {
            screen.Action = new myapp.Action();
        }
        screen.showPopup("ActionPopup");

    If your scenario involves navigation among screens, you can try Michael's blog post on the topic.

    Feel free to post more details of your scenario if you're still blocked.

    Best regards,
    Huy

    Tuesday, July 9, 2013 11:14 PM
  • Many thanks for responding.

    This is a link to the screen-shot. Hopefully it will be clearer if viewed in a browser:

    https://dl.dropboxusercontent.com/u/10788935/Picture2.png

    I inserted your code (see below), but the numeric display in the pop-up is still greyed-out and I cannot enter a value (see above screen-shot).

    So I suspect that the record is not bound to the control. Can you please discuss how I should proceed because all I am trying to do is enter a numeric value into the existing record or if one doesn't exist to create one and enter the numeric value.

    Thanks and regards, Mark.

    myapp.ViewStoreGroup.ProjectQuestionsStore_ItemTap2_execute = function (screen) {
        //  If there is no ActionItem then add one otherwise the PopUp should be bound to the selected ActionItms
    
        if (!screen.ActionItem) {
            screen.ActionItem = new myapp.ActionItem();
        }
    
        // Show the PopOp which shows the numeric field in the selected ActionItem
        screen.showPopup("NumericPopUpStore");
    
        // I suspect the code below is never executed because the PopUp terminates this code ???
        // But I also suspect that the new record isn't saved because the numeric input field remains grayed-out as shown in the above screen-shot.
        return myapp.activeDataWorkspace.ApplicationData
        then.saveChanges()
    };
    
    myapp.ViewStoreGroup.ActionItem_selectedItem_NumberAnswer_postRender = function (element, contentItem) {
        // Write code here.
        $(element).find("input").get(0).type = "number";
    };


    Mark

    Wednesday, July 10, 2013 3:35 AM

  • Notice the pop-up is greyed out. Thanks, Mark.

    • Edited by ITPSB Wednesday, July 10, 2013 3:49 AM typo
    Wednesday, July 10, 2013 3:47 AM
  • Hi Mark,

    Much better with the new picture, thanks.

    Based on your screenshot, ActionItem is not a single item, but a visual collection representing a collection of Action Items. So your code should be:

    myapp.ViewStoreGroup.ProjectQuestionsStore_ItemTap2_execute = function (screen) {
        if (!screen.ActionItem.selectedItem) {
            screen.ActionItem.selectedItem = 
                screen.ActionItem.addNew();
        }
    
        screen.showPopup("NumericPopUpStore");
    
        $(window).one("popupafterclose", function(){
           myapp.activeDataWorkspace
               .ApplicationData.saveChanges();
        });
    };
    
     

    Regarding your save code:

    • The syntax is wrong so that'd probably why nothing is saved. I'm surprised you did not get an error.
    • I think your intention is to wait until the popup is close to do the save, not immediately save after the popup is opened. To do so you need to add an event handler.

    Also, is it possible that your screen is a little bit complex and can be broken down? You can take a look at this blog post and see if it may help you with alternate solutions.

    Best regards,
    Huy

     

    Wednesday, July 10, 2013 5:29 PM
  • Wow! Fantastic support, thank you very much for assisting.

    I think I'm almost there.

    You are correct in saying "think your intention is to wait until the popup is close to do the save, not immediately save after the popup is opened", so when you say "you need to add an event handler", I assume you mean add a "Save/Cancel" button. Am I correct?

    The work flow follows Andy Kung's "Creating a wizard-like experience for HTML client" and it seems to be working out well.

    In my app, audit questions are created in the SL client and the HTML users answer the audit questions by tapping to select an area being audited and then tapping the questions to respond to them (for the selected area).

    Again, many thanks indeed for your assistance.


    Mark

    Thursday, July 11, 2013 9:26 AM
  • Hi Mark,

    The sample code already contains the event handler - $(window).one("popupafterclose", function(){ . That handler will fire once after the current pop up is closed and all the existing changes will be saved. Keep in mind when you call saveChanges all changes will be persisted, not just the newly added or edited Action Item. So it may be safer to let the user explicitly saving the changes using Save / Cancel.

    Best regards,
    Huy

    • Marked as answer by ITPSB Friday, July 12, 2013 2:04 AM
    Thursday, July 11, 2013 3:17 PM