locked
HTML Client if has data RRS feed

  • Question

  • Sorry if this has been asked, I did read through first but I can't quite figure out what to search for since these are really general terms.

    I'm starting off making an internal work tracker for stuff like contacts, jobs done (Service desk style), invoices, quotes, etc. First thing's first, I'm setting up my contacts table. That's all well and good, I have a Contact table with a 1 - Many relationship with the Address/Phone/Email tables and that all works beautifully.

    What I'm trying to do is hide those lists if there's nothing in them for that contact. I thought I could just do this after I stole it from another thread (Element is hidden by default by the designer):

    myapp.ViewContact.created = function (screen) {
        function updateAddressVisibility() {
            if (screen.Contact.Address.length > 0) {
                screen.findContentItem("Address").isVisible = true;
            } else {
                screen.findContentItem("Address").isVisible = false;
            }
        }
        screen.Contact.addChangeListener("Address", updateAddressVisibility);
        updateAddressVisibility();
    };

    But it turns out that screen.Contact.Address is an object and not an array. The usual fix would be to use ..Address.keys().length.. but that doesn't work either. The above code does work, but the problem is that it also works for an empty Address. So somehow the Address object has a length > 0 anyway.

    So what am I missing from the code to hide any empty elements?
    Tuesday, June 3, 2014 8:01 AM

Answers

  • Others might have more and better to contribute, but I believe you'll have to query for those records.  As you've seen, the Address object is just that, and not an array.

    The entities on the screen have 'getter' methods associated with them-- I'm doing something similar to what you want;  on a page with people I check whether or not they're also employees and set a checkbox if so:

    myapp.BrowsePeople.IsEmployee_render = function (element, contentItem) {
        contentItem.value.getEmployee().then(function success(result) {
            if (result !== undefined) { //already has an employee record
                //$(element).buttonMarkup({
                //    icon: "check"
                //});
                $element = $(element);
                //var $textbox2 = $('<a href="#" data-role="button" data-icon="delete" data-iconpos="notext">Delete</a>');
                //$element.append($textbox2);
                $element.append($('<a href="#" data-role="button" data-icon="check" data-iconpos="notext"></a>'));
            }
        }
    );
    };

    The key takeaway is the getEmployee() function above-- I didn't write it, it came with the Employee entity.  You likely have a getAddress() method available on the address entity.

    Note the asynchronous / use of a promise in the above code.  It's a familiar pattern for the kind of 'does it exist/have X value' types of questions we have in our apps.  An example from the API documentation can be seen here, in this case on the screen object---

    http://msdn.microsoft.com/en-us/library/dn720458.aspx#methods




    • Edited by jim bancroft Tuesday, June 3, 2014 8:41 PM
    • Marked as answer by LastElf Wednesday, June 4, 2014 2:17 AM
    Tuesday, June 3, 2014 8:39 PM

All replies

  • Others might have more and better to contribute, but I believe you'll have to query for those records.  As you've seen, the Address object is just that, and not an array.

    The entities on the screen have 'getter' methods associated with them-- I'm doing something similar to what you want;  on a page with people I check whether or not they're also employees and set a checkbox if so:

    myapp.BrowsePeople.IsEmployee_render = function (element, contentItem) {
        contentItem.value.getEmployee().then(function success(result) {
            if (result !== undefined) { //already has an employee record
                //$(element).buttonMarkup({
                //    icon: "check"
                //});
                $element = $(element);
                //var $textbox2 = $('<a href="#" data-role="button" data-icon="delete" data-iconpos="notext">Delete</a>');
                //$element.append($textbox2);
                $element.append($('<a href="#" data-role="button" data-icon="check" data-iconpos="notext"></a>'));
            }
        }
    );
    };

    The key takeaway is the getEmployee() function above-- I didn't write it, it came with the Employee entity.  You likely have a getAddress() method available on the address entity.

    Note the asynchronous / use of a promise in the above code.  It's a familiar pattern for the kind of 'does it exist/have X value' types of questions we have in our apps.  An example from the API documentation can be seen here, in this case on the screen object---

    http://msdn.microsoft.com/en-us/library/dn720458.aspx#methods




    • Edited by jim bancroft Tuesday, June 3, 2014 8:41 PM
    • Marked as answer by LastElf Wednesday, June 4, 2014 2:17 AM
    Tuesday, June 3, 2014 8:39 PM
  • I haven't tried this to confirm, but I believe the .value object would be undefined if the property is not set.

      So try this: 

     if (screen.Contact.Address.value ) {...

    Jim is correct regarding navigation properties being deferred so you'd call the getAddresses() if you needed to get the count of addreses for example.

    HTH,

    Josh

    Tuesday, June 3, 2014 11:41 PM
  • Does

    if (screen.Contact.Address != null) { }

    not work?

    Wednesday, June 4, 2014 12:18 AM
  • This works beautifully! I had to make a small edit to my change listener as well by making it watch screen.Address.count instead of "Address" and I'm now using Address.count instead of value, but besides that it's now perfect.

    Thanks very much, Jim!
    • Edited by LastElf Wednesday, June 4, 2014 2:19 AM
    Wednesday, June 4, 2014 2:17 AM