Quantcast
Viewing all articles
Browse latest Browse all 26374

Processing sp list async queries inside control template using JavaScript

Hi,

Im trying to make custom lookup field with JSLink processing its templates, but im not guru in JS so i need your help :)

Well, im trying to make js "class" that should take as input ctx data, query sp for list items and fill given combobox. Important thing is that this class will have a lot of instances, because it should be created during custom field template...

function GetFieldControlPrefix(ctx) {
    return ctx.CurrentFieldSchema.Name + '_' + ctx.CurrentFieldSchema.Id;
}

function AdvLookupQueryProcessor(ctx) {
    this.Ctx = ctx; // save for later use
    this.ControlsPrefix = GetFieldControlPrefix(ctx); // get unique control name
    this.QueryResults = null; // to save query results


AdvLookupQueryProcessor.prototype.ExecuteQuery = function ()
{
    // Load context
    var spContext = new SP.ClientContext.get_current();

    // Get lookup list
    var spLookupList = spContext.get_web().get_lists().getByTitle("TestList2");

    // Prepare query
    var spCamlQuery = new SP.CamlQuery();
    var spViewXML = '<View><RowLimit>99999</RowLimit></View>';
    spCamlQuery.set_viewXml(spViewXML);

    // Prepare result items
    this.QueryResults = spLookupList.getItems(spCamlQuery);

    // Execute query on list
    spContext.load(this.spQueryResults);
    spContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFail));
};

// onQuerySuccess
AdvLookupQueryProcessor.prototype.onQuerySuccess = function () {
    alert ("OK for " + this.ControlPrefix )
};

// onQueryFail
AdvLookupQueryProcessor.prototype.onQueryFail = function () {
    alert("fail for " + this.ControlPrefix)
};

If im using 

...Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFail ...

im getting error that object was not found, without deligate everything seems fine but onQuerySuccess never fires.

This is how class is created during template processing 

    function AdvLookupFieldEditTemplate(ctx) {
        var sHtml = ""; // output html string

        ....

        // write select box
        this.SelectBoxId = GetFieldControlPrefix(ctx) + "_SelectBox";
        sHtml += "<select id='" + this.SelectBoxId + "' ></select>";

....
        // Fill lookup with data
        var proc = new AdvLookupQueryProcessor(ctx);
        ExecuteOrDelayUntilScriptLoaded(proc.ExecuteQuery, "sp.js");

        return sHtml;
    }

Any ideas? Thanks!





Viewing all articles
Browse latest Browse all 26374

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>