locked
Preferred way of adding child records in HTMLClient javascript RRS feed

  • Question

  • Hi,

    What is the preferred way of adding child records to an on-screen entity using javascript code?

    Case: I have an edit screen with an entity "Activity" and I want to add a new item to the collection "Activity.Documents" as well as operating on the fields of the new "Document". All without leaving the current "Activity" edit screen.

    An example would be welcome.

    Best regards

    JensBo

    Wednesday, April 24, 2013 7:17 AM

Answers

  • Hi Huy,

    That's service mindedness!

    Anyway, in stead of using your time I think I will do a complete uninstall of VS and all related programs and then a new installation. As mentioned in another thread I also have problems adding HTMLClients to projects (converted as well as new). I installed the dev environment on a fresh test machine and this problem was not present there. So I suspect that someting "preview-update-history-related" error is going on on my development machine.

    In the meantime I have found a workable solution - just not with debug.

    Best regards

    JensBo

    • Marked as answer by Angie Xu Wednesday, May 8, 2013 12:40 AM
    Thursday, May 2, 2013 7:09 AM

All replies

  • Hi JensBo,

    "I want to add a new item to the collection "Activity.Documents" as well as operating on the fields of the new "Document".

    If you want to do this purely through code (no UI involved), you can create a new Document, set its properties and as part of it assign its Activity to the current activity. For example:

    var newDocument = new myapp.Document();
    newDocument.Property1 = "...";
    newDocument.Property2 = "...";
    // Assuming the reference property name is Activity
    newDocument.Activity = screen.Activity;

    If you want to add the Document but also involves UI, I would recommend using the built-in Add Edit New button, which will involve another screen. But since you're already on an Add/Edit Activity screen, the new Add/Edit Document screen will be in a nested scope, so you don't have to save the new Document before leaving its screen.

    Best regards,
    Huy Nguyen


    • Edited by Yann DuranModerator Tuesday, April 30, 2013 1:07 PM Italicised the quoted text, to make it more obvious it was quoted
    Wednesday, April 24, 2013 4:39 PM
  • Hi again Huy,

    Thank's for the response.

    Your code works as expected in case I use it in a button handler. But in other cases I have problems.

    Example: (Adding a custom control to grab a picture and upload)

    myapp.ViewActivity.AddPicture_render = function (element, contentItem) {
         $(element).append('<input type="file" id="myPicture" accept="image/*" capture="camera">');
         document.getElementById('myPicture').addEventListener('change', function () {
             var files = this.files;
             uploadImage(files[0], element, contentItem);
         }, false);
     };
    
    uploadImage = function (file, element, contentItem) {
         var newDocument = new  myapp.Document();
         newDocument.Name = "hhh";
         newDocument.Activity = contentItem.screen.Activity; 
     } //Breakpoint here//

    In this example newDocument.Name is "undefined" at the breakpoint even as it has been assigned two lines up without error.

    (I am new to javascript and i know that my error could be trivial)

    Best regards

    JensBo


    Thursday, April 25, 2013 6:19 AM
  • Hi JensBo,

    That is very strange. I'm not entirely sure why without debugging the code, so I can only try trouble-shooting steps.

    What happened if you set a breakpoint on 'newDocument.Activity = contentItem.screen.Activity;'? Is newDocument.Name 'hhh' at that point?

    Under the App.HtmlClient folder, there should be a GeneratedArtifact folder, can you open the data.js file and post the generated code under msls._addToNamespace("msls.application", {...?

    Best regards,
    Huy Nguyen

    Thursday, April 25, 2013 4:13 PM
  • Hi again,

    Moving the breakpoint doesn't make any difference.

    Here are some snippets from msls.application part of the GeneratedArtifact file. (I have tried to include only the relevant parts as the actual code is quite long). If you can use the complete file I can e-mail it to you.

            Document: $defineEntity(Document, [
                { name: "ID", type: String },
                { name: "Name", type: String },
                { name: "FileType", type: String },
                { name: "CreatedOn", type: Date },
                { name: "ChangedOn", type: Date },
                { name: "CreatedBy", type: String },
                { name: "ChangedBy", type: String },
                { name: "ExternalStoreType", type: String },
                { name: "ExternalStoreLocation", type: String },
                { name: "Activity1", kind: "reference", type: Activity },
                { name: "DocumentStore1", kind: "reference", type: DocumentStore }
            ]),

    //********************************//

            DocumentStore: $defineEntity(DocumentStore, [
                { name: "ID", type: String },
                { name: "FileData", type: Array },
                { name: "Documents", kind: "collection", elementType: Document }
            ]),

    //********************************//

                {
                    name: "Documents_SingleOrDefault", value: function (ID) {
                        return new $DataServiceQuery({ _entitySet: this.Documents },
                            lightSwitchApplication.rootUri + "/SkyViewCRMData.svc" + "/Documents(" + "ID=" + $toODataString(ID, "Guid?") + ")"
                        );
                    }
                },

    //********************************//

            DataWorkspace: $defineDataWorkspace(DataWorkspace, [
                { name: "SkyViewCRMData", type: SkyViewCRMData },
                { name: "XtraReportsServiceData", type: XtraReportsServiceData },
                { name: "BI_RIA_ServiceData", type: BI_RIA_ServiceData }
            ])

    //********************************//

    Best regards

    JensBo

    sdsd

    Monday, April 29, 2013 6:23 AM
  • Hi JensBo,

    First issue with your code above:

            Document: $defineEntity(Document, [
                . . . .
                { name: "Activity1", kind: "reference", type: Activity },
                { name: "DocumentStore1", kind: "reference", type: DocumentStore }
            ]),

    newDocument.Activity = contentItem.screen.Activity; will not work. You need to use Activity1, not Activity.

    newDocument.Name is "undefined"

    I still cannot explain why this line does not work. Can you set a breakpoint on newDocument.Name = "hhh";? When the breakpoint is hit, can you use the watch window and let me know these values:

    • newDocument.details - does this object exists?
    • newDocument.details.entityState
    • newDocument.details.entitySet
    • newDocument.details._.Name

    Then step through the line newDocument.Name = "hhh"; and see if those values change.

    Best regards,
    Huy Nguyen

    Monday, April 29, 2013 4:47 PM
  • Hi Huy,

    At the breakpoint on newDocument.Name = "hhh" I can see:

    • newDocument.details - does this object exists? Yes
    • newDocument.details.entityState Not there, but newDocument.details._._entityState = "added"
    • newDocument.details.entitySet Is there. And seems ok.
    • newDocument.details._.Name No properties of newDocument are listet under newDocument.details._.. For other objects (like the referenced Activity) all properties are there. Just not for the added newDocument.

    Nothing seems to change when I step further.

    The above results are taken using the code shown at the start of this thread. Since then I have tried a modified version in which I still cannot inspect the values during debug, but evenso the fields are updated and saved to the database.

    Best regards

    JensBo


    • Edited by JensBo Tuesday, April 30, 2013 11:19 AM
    Tuesday, April 30, 2013 11:18 AM
  • Hi JensBo,

    It looks like the newDocument object was created correctly. I cannot figure out why newDocument.Name = "hhh" does not work.

    The above results are taken using the code shown at the start of this thread. Since then I have tried a modified version in which I still cannot inspect the values during debug, but evenso the fields are updated and saved to the database.

    Does this mean you were able to get your application to work? Otherwise, if you can share your project (or a simplified version of it that shows the error) I will try to take a look when I have a chance.

    Best regards,
    Huy Nguyen

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

    That's service mindedness!

    Anyway, in stead of using your time I think I will do a complete uninstall of VS and all related programs and then a new installation. As mentioned in another thread I also have problems adding HTMLClients to projects (converted as well as new). I installed the dev environment on a fresh test machine and this problem was not present there. So I suspect that someting "preview-update-history-related" error is going on on my development machine.

    In the meantime I have found a workable solution - just not with debug.

    Best regards

    JensBo

    • Marked as answer by Angie Xu Wednesday, May 8, 2013 12:40 AM
    Thursday, May 2, 2013 7:09 AM