0

I am using this code:

"use strict"
var BasicJSOM = window.BasicJSOM || {};
BasicJSOM.Crud = BasicJSOM.Crud || {};

$(function() {
   BasicJSOM.Crud.MainDemo = new BasicJSOM.Crud.Main();
SP.SOD.executeOrDelayUntilScriptLoaded(BasicJSOM.Crud.MainDemo.readListItems("pages"), "SP.js");   
  });


 BasicJSOM.Crud.Main= function(){

    function _readListItems(library){

       var context = SP.ClientContext.get_current();
       var targetList = context.get_web().get_lists().getByTitle(library);
       var query = new SP.CamelQuery();
        var listItems = targetList.getItems();

        results = context.loadQuery(listItems);
        context.executeQueryAsync(_onSucceed, _Fail);

       function _onSucceed(){
            alert("Success"); 

       }

       function _Fail(){
       alert("fail"); 
       }

    }//end of read list


    return {
readListItems: _readListItems    
    }

 } 

But the error am having is about the SP.js is not loaded, I am using jQuery to wait for page load, then using SP.SOD.executeOrDelayUntilScriptLoaded. What else am I missing?

Brittany Rutherford
  • 878
  • 2
  • 16
  • 35

1 Answers1

3

You should specify a function as the first parameter of the executeOrDelayUntilScriptLoaded method, like BasicJSOM.Crud.MainDemo.readListItems. If you use BasicJSOM.Crud.MainDemo.readListItems("pages"), the function is executed immediately, that means probably before the SP.js library loaded.

If you need to pass a parameter to the method, use the following syntax:

SP.SOD.executeOrDelayUntilScriptLoaded(function () { BasicJSOM.Crud.MainDemo.readListItems("pages") }, "SP.js");   

See this question as reference.

pholpar
  • 3,190
  • 1
  • 15
  • 14
  • Thanks @pholpar, do I need to use SP.SOD.executeOrDelayUntilScriptLoaded or just right away executeOrDelayUntilScriptLoaded, what's the difference? Also do I have to use executeFunc? – Brittany Rutherford Jan 23 '15 at 19:44
  • 1
    See this thread for details: http://sharepoint.stackexchange.com/questions/58503/sp-sod-how-to-use-correctly – pholpar Jan 23 '15 at 20:42