locked
Return data from a Javascript Success Handler RRS feed

  • Question

  • Hi folks!! I need help, so this is my point:

    I developed a js lib that interact with lists in sharepoint via, obviously, Javascript Object Model. In my feature i include my lib via CustomAction. I need get the data returned from the show list function and insert in a div, but my function, as already said, is in another file. There is some way to do this ? Here is my code:

     

    var cc, web;
    
    // Auxiliar functions 
    
    function loadClientContext() {
        loadContext(cc);
        loadWeb(web);
    }
    function loadContext(c) {
        if (cc != null && cc != undefined) {
            return cc;
        } else {
            cc = new SP.ClientContext.get_current();
            return c;
        }
    }
    function loadWeb(w) {
        if (web != null && web != undefined) {
            return web;
        } else {
            web = cc.get_web();
            return web;
        }
    }
    
    // --------------------------
    
    function show(list) {
        this.operation = 'show';
        SP.SOD.executeOrDelayUntilScriptLoaded(loadClientContext, 'sp.js');
        this.localList = web.get_lists().getByTitle(list);
        if (this.localList != null) {
            var query = new SP.CamlQuery.createAllItemsQuery();
            this.items = this.localList.getItems(query);
            cc.load(this.items);
            cc.executeQueryAsync(Function.createDelegate(this, this.onSucess), Function.createDelegate(this, this.onError));
    
            // This is returning data from Sucess Handler ?
            return this.objResult;
    
        } else {
            SP.UI.Notify.addNotification(msgListaInexistente, false);
        }
    }
    
    function onSucesso() {
        if (this.operation == 'insert') {
            SP.UI.Notify.addNotification('Sucess', false);
        } else if (this.operation == 'show') {
            var enumerator = this.items.getEnumerator();
            var i = 0;
            this.objResult = [];
            while (enumerator.moveNext()) {
                var actualItem = enumerator.get_current();
                this.objResult[i++] = { Title: actualItem .get_item('Title'), StartDate: actualItem.get_item('StartDate') };
            }
        }
    
    }
    

     

    And i want invoke the show function like this:

     

    $(function(){
       // Call to show function in my js library
       var tasks = show('Tasks');
       for(var r in tasks)
      {
           $('#someDiv').html('Task:' + tasks[r].Title + '<br/>' + 'Start Date:' +  tasks[r].StartDate);
      }
    });
    

     

    Anyone can help?

    Thanks


    http://marcosnatan.wordpress.com
    • Edited by mnatan.brito Saturday, January 14, 2012 9:52 PM Title wrong
    Saturday, January 14, 2012 3:27 PM

Answers

  • OK one question.... Can you not do this?

    Let me know if this kinda model can be possible for you...

     

    
    
    //No need for Global variable in this case
    //var RESULT;
    $(function(){
       // only Call to show function in my js library
       show('Tasks');
      
    });
    
     //A separate function for data rendering
     function RenderData(RESULT)
    {
     for(var r in RESULT)
      {
           $('#someDiv').html('Task:' + RESULT[r].Title + '<br/>' + 'Start Date:' +  RESULT[r].StartDate);
      }
    }
    
    and returning the data from the success handler
    
     
    
    function onSuccess() {
        if (this.operation == 'insert') {
            SP.UI.Notify.addNotification('Success', false);
        } else if (this.operation == 'show') {
            var enumerator = this.items.getEnumerator();
            var i = 0;
            this.objResult = [];
            while (enumerator.moveNext()) {
                var actualItem = enumerator.get_current();
                this.objResult[i] = { Title: actualItem .get_item('Title'), StartDate: actualItem.get_item('StartDate') };
                i += 1;
            }
           //Call the funtion to render the data found on success from this handler
              RenderData(objResult);
        }
    
    }
    



     


    "T" | My blog updates | My Twitter | Our Products | Mail Me | LinkedIn
    • Edited by Tanmay Shahane Sunday, January 15, 2012 4:34 PM code format
    • Marked as answer by mnatan.brito Monday, January 16, 2012 12:20 AM
    Sunday, January 15, 2012 4:33 PM

All replies

  • Hi,

    Check this http://blogs.msdn.com/b/chaks/archive/2011/09/14/modal-dialog-box-in-sharepoint-sandbox.aspx

    I am sure this will solve your problem.Let me know if any issue


    Regards Ravi http://sharepointwithravi.blogspot.com
    Saturday, January 14, 2012 8:02 PM
  • Thanks for your reply, but i need a way to pass data back to the function that primarily called the function that catch some items in the list not to the sucess handler of the executeQueryAsync function.
    http://marcosnatan.wordpress.com
    Saturday, January 14, 2012 8:56 PM
  • Hi Mnatan,

     

    So this is what is happening here... the Success handler is a  Async call and not a sync call so when you call the Showlist function... it calls a executeQuery function to fetch data... now since this is a aysnc call it does not mean that you show list function execution will wait in queue ... so if you define a Local variable in ShowList function... populate it in success handler and return the object in the ShowList function to the primary calling function.. its not necessarily will have the required data.... (Hope my writing  not too confusing :) ).. does that make sense?

     

    Now what alternatively you can do is... you can create a global variable or a global object or class or struct which can be in your file having the primary function... now since that file is referenced before this ShowList function too (hopefully) then that variable will also  be available.. now you can populate that variable and then it can be available to your primarly calling function as well... AGAIN... success handler is async so it wont stop the execution till its over/// so you have to include proper checks....

     

    Hope this all makes sense and you can get some idea on what can be possible options...

     

    - "T"


    "T" | My blog updates | My Twitter | Our Products | Mail Me | LinkedIn
    Sunday, January 15, 2012 12:56 PM
  • Ok thanks for the reply, i get the idea but how i could implement this? Just adding a global variable to

     

    // Global variable
    var RESULT;
    $(function(){
       // Call to show function in my js library
       RESULT = show('Tasks');
       for(var r in RESULT)
      {
           $('#someDiv').html('Task:' + RESULT[r].Title + '<br/>' + 'Start Date:' +  RESULT[r].StartDate);
      }
    });
     
    

     

    and returning the data from the success handler

     

    function onSuccess() {
        if (this.operation == 'insert') {
            SP.UI.Notify.addNotification('Success', false);
        } else if (this.operation == 'show') {
            var enumerator = this.items.getEnumerator();
            var i = 0;
            this.objResult = [];
            while (enumerator.moveNext()) {
                var actualItem = enumerator.get_current();
                this.objResult[i] = { Title: actualItem .get_item('Title'), StartDate: actualItem.get_item('StartDate') };
                i += 1;
            }
        }
    
    }
     
    

     


    this will make it work? Can you give me some code example? Or there is some way to make a Sync call ?

    Thanks!

     


    http://marcosnatan.wordpress.com
    • Edited by mnatan.brito Sunday, January 15, 2012 1:54 PM Add question
    Sunday, January 15, 2012 1:53 PM
  • OK one question.... Can you not do this?

    Let me know if this kinda model can be possible for you...

     

    
    
    //No need for Global variable in this case
    //var RESULT;
    $(function(){
       // only Call to show function in my js library
       show('Tasks');
      
    });
    
     //A separate function for data rendering
     function RenderData(RESULT)
    {
     for(var r in RESULT)
      {
           $('#someDiv').html('Task:' + RESULT[r].Title + '<br/>' + 'Start Date:' +  RESULT[r].StartDate);
      }
    }
    
    and returning the data from the success handler
    
     
    
    function onSuccess() {
        if (this.operation == 'insert') {
            SP.UI.Notify.addNotification('Success', false);
        } else if (this.operation == 'show') {
            var enumerator = this.items.getEnumerator();
            var i = 0;
            this.objResult = [];
            while (enumerator.moveNext()) {
                var actualItem = enumerator.get_current();
                this.objResult[i] = { Title: actualItem .get_item('Title'), StartDate: actualItem.get_item('StartDate') };
                i += 1;
            }
           //Call the funtion to render the data found on success from this handler
              RenderData(objResult);
        }
    
    }
    



     


    "T" | My blog updates | My Twitter | Our Products | Mail Me | LinkedIn
    • Edited by Tanmay Shahane Sunday, January 15, 2012 4:34 PM code format
    • Marked as answer by mnatan.brito Monday, January 16, 2012 12:20 AM
    Sunday, January 15, 2012 4:33 PM
  • Hi Tanmay, thanks for your tip. This code worked perfectly. Thanks
    http://marcosnatan.wordpress.com
    Monday, January 16, 2012 12:22 AM