locked
Is it possible to initialize a screen variable for use in control postrender code RRS feed

  • Question

  • Here's the scenario: three tables - LicenseKey, Subscription, SubscriptionDomain. LicenseKey has a Subscription which is associated with a SubscriptionDomain. In an add/edit LicenseKey screen I need to use LicenseKey.Subscription.SubscriptionDomain.Id field in a number of control postrender blocks. I created a screen property called currentSubscriptionID and initialize it as such:

    myapp.LicenseKeyAddEdit.created = function (screen) { // ...

    screen.LicenseKey.Subscription.getSubscriptionDomain().then( Init_inpAppSystem , function (error) { msls.showMessageBox(error, { title: "Error loading SubscriptionDomain from database." }); }); function Init_inpAppSystem(SubscrDommain) { screen.currentSubscriptionID = SubscrDommain.Id; // ... } // ... };

    The problem is when I use it currentSubscriptionID in a control's postRender block, it is sometimes null, (not initialized) - example below:

    myapp.LicenseKeyAddEdit.inpLicenseType_postRender = function (element, contentItem) {
    
        contentItem.dataBind("screen.LicenseKey.AppSystem", function () {
            if (contentItem.screen.LicenseKey.AppSystem != null) {
                // PROBLEM: contentItem.screen.currentSubscriptionID is null the first time around!!!
                var filter = "SubscriptionDomainId eq " + msls._toODataString(contentItem.screen.currentSubscriptionID, ":Int32")
                            + " and AppSystem eq " + msls._toODataString(contentItem.screen.LicenseKey.AppSystem, ":String");
    
                // ...
                })
            };
        });
    };

    I'm guessing that the postRender code is executing before the LicenseKeyAddEdit.created code has had a chance to initialize the variable. How should this be handled? Is there a way to initialize a variable before anything else happens?

    Any assistance in this matter will be greatly appreciated.

     

    Bruce


    Bruce

    Wednesday, July 2, 2014 1:56 PM

Answers

  • Maybe you could use dataBind to also listen for change on currentSubscriptionID like:

    contentItem.dataBind("screen.LicenseKey.AppSystem", function (value) {
    	if (value != null) {
    		 setFilterContinue(contentItem.screen)
    	};
    });
    contentItem.dataBind("screen.currentSubscriptionID", function (value) {
            if (value != null) {
                 setFilterContinue(contentItem.screen)
            };
        });
    function setFilterContinue(screen){       
    	if (screen.currentSubscriptionID != null &&  screen.LicenseKey.AppSystem != null){  
    	var filter = "SubscriptionDomainId eq " + msls._toODataString(screen.currentSubscriptionID, ":Int32")
    				+ " and AppSystem eq " + msls._toODataString(screen.LicenseKey.AppSystem, ":String");
    
    				// ...
    	}
    }

    HTH,

    Josh

    • Marked as answer by bruce demoose Wednesday, July 9, 2014 5:47 PM
    Wednesday, July 9, 2014 5:03 PM

All replies

  • Maybe you could use dataBind to also listen for change on currentSubscriptionID like:

    contentItem.dataBind("screen.LicenseKey.AppSystem", function (value) {
    	if (value != null) {
    		 setFilterContinue(contentItem.screen)
    	};
    });
    contentItem.dataBind("screen.currentSubscriptionID", function (value) {
            if (value != null) {
                 setFilterContinue(contentItem.screen)
            };
        });
    function setFilterContinue(screen){       
    	if (screen.currentSubscriptionID != null &&  screen.LicenseKey.AppSystem != null){  
    	var filter = "SubscriptionDomainId eq " + msls._toODataString(screen.currentSubscriptionID, ":Int32")
    				+ " and AppSystem eq " + msls._toODataString(screen.LicenseKey.AppSystem, ":String");
    
    				// ...
    	}
    }

    HTH,

    Josh

    • Marked as answer by bruce demoose Wednesday, July 9, 2014 5:47 PM
    Wednesday, July 9, 2014 5:03 PM
  • Josh:

    That worked, many thanks. I didn't realize two databind statements could be applied to the same contentItem.

    Bruce


    Bruce

    Wednesday, July 9, 2014 5:47 PM
  • Bruce,

    You're welcome. 

    dataBind basically just adds a change listener on the property specified in the first argument (binding path) and then, on change, executes the function in the second argument (callback) passing the newValue as argument.

    The relationship to contentItem is more about scope\context since the binding path is resolved in the context of contentItem.

    For example,

    The following will listen for changes on contentItem.Value

    contentItem.dataBind("Value", function (newValue)...

    while the following will listen for changes on contentItem.screen.somePropertyName

    contentItem.dataBind("screen.somePropertyName", function (newValue)...

    The contentItem doesn't necessarily have to be involved in the code that runs in the callback function.

    Josh

    Wednesday, July 9, 2014 6:13 PM