Currentitem undefined in new function ( Deffered)

488 Views Asked by At

I am editing a item display template in sharepoint. the following problem occurs:

ctx(clientcontext).currentitem becomes undefined in a custom function. how can i solve this.

Here is a litle code snippet:

 var p = getFollowers();
 console.log("")
 console.debug(ctx.CurrentItem);
 p.done(function(result) {
  console.debug(followers);
  console.debug(ctx);
 });

I don't see what i miss here

edit 1:

                var results;
    function successCallback() {
        console.debug("test");
        var clientContext = new SP.ClientContext.get_current();
        var counts = 0;
        var test = results.getEnumerator();
        while (test.moveNext()) {
        var person = test.get_current();
            followers.push(person.get_displayName());
            counts++;
        }
        $("#followers").text('(' + counts + ')');   
        this.d.resolve(followers);
    }

    function failCallback() {
        this.d.reject("something bad happened");
    }

    function getFollowers()
    {
        var d = $.Deferred();
        var clientContext = new SP.ClientContext.get_current();
        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

        // Get the people who are following the current user.
        peopleFollowingMe = peopleManager.getMyFollowers();

        clientContext.load(peopleFollowingMe);
        results = peopleFollowingMe;

        var o = {d: d, results:results};

        clientContext.executeQueryAsync(Function.createDelegate(o, successCallback), Function.createDelegate(o, failCallback));
        return d.promise();
    }
1

There are 1 best solutions below

3
On

I am not an expert of sharepoint. I am wondering if the ctx variable can be modified by the p.done().

Why don't you store the CurrentItem before calling p.done() ?

 var p = getFollowers();
 console.log("");
 var currentItem = ctx.CurrentItem;
 console.debug(currentItem);
 p.done(function(result) {
     /* console.debug(followers); */
     console.debug(currentItem);
 });

By the way. What is it the followers variable that you are reading in the callback ?