Async Code Data Provider

Async Code Data Provider

General Info

This type of data provider has a similar logic with the Code Data Provider, in that it allows you to write javascript code and feed your controls and properties with a completely  custom value on runtime. However, instead of expecting a “return” statement in order to provide the value, it requires a “callback” method to be called. This method has to be invoked by passing an object with the property “Data” as an argument (ie callback({Data: "value 1"}). The main advantage of this approach, is that it allows you to execute asynchronous calls in your code (jquery/ajax calls, web services, timeout functions), and wait for them to be executed before the provider returns its data. 

The form special variable is available in order to provide access to the parent form control/object, its methods and all included controls.

Examples

The following Async Code Provider is used to feed the Value property of a read-only Textbox (“CountryTitle”), based on another Textbox control (“CountryID”), that expects the ID of a list item in a list named “Countries”. This is a typical scenario for this kind of provider, where the user inputs a value in the first textbox (“CountryID”), and an async method (in our case pf.sp.DataLoader.LoadRecord function), fetches the Title if a given list item. 


var
listName = "Countries"; var countryID = form.GetControl("CountryID").GetValue(); if (!countryID) { callback({Data: "No ID given yet"}); //return empty string if no ID is given return; } //Load the Country for the given ID var loader = new pf.sp.DataLoader(); loader.LoadRecord({ ListTitle: listName, Id: countryID }, function(e) { if (e.Error) { alert("Error: " + e.ErrorMessage); callback({Data: ""}); return; } var country = e.Data; if (country) { //if a record is found, return its title callback({Data: country.Title}); } });

This results are similar to the following:

  1.   (no value)

  1. (ID given)

See Also

For more info about the concept of Data Providers, their usage and their common properties (Name, OnDataLoaded, Condition, etc) see Basic Concepts: Intro to Data Providers 
For more info about Code and Scripting customization capabilities in Openous, see Introduction to Openous Events and Scripting