3

I declare a function and assigned it to a Variable, inside the function was other functions. Normally, at first load of the page it loads the functions etc. When i used the variable in an ajax success callback, it says that the init() function is not a function.

This is my sample jquery function assigned to a variable called GRID:

    var Grid = (function() {
       function init( config ) { ... }
         .
         .
         .
       function addItems( $items ) { ... }
      return { init: init, . . . addItems: addItems }
    }

();

and i have a this ajax call

     function updateItems{
      .
      .
      .
      .
       jQuery.ajax({

            type: 'get',
            url:  ajaxURL,
            data: ajaxData,
            dataType: 'json',

            //Ajax call is successful
            success: function ( response ) {
                //Add new posts
               Grid.init();

            },
            error: function () {

            }
        });
        .
        .
        .
        }

What's wrong with the codes? why would the Inspector returns an error that says Grid.init() is not a function? please help

9
  • 2
    Grid is not a function, nor an object, the function looks like it's an IIFE, otherwise why does it start with a parentheses, that returns undefined unless you explicitly return something, and you can't call that function later on Commented Feb 19, 2016 at 18:49
  • is var Grid declared in the same scope (containing function, or global scope) as updateItems? Commented Feb 19, 2016 at 18:56
  • var Grid is declared as global scope and also updateItems. @billy what's wrong with my code? Commented Feb 19, 2016 at 18:58
  • do you call your grid function whilst defining it... var Grid = (function() { ... })(); <~~ note the calling braces Commented Feb 19, 2016 at 19:00
  • Yes. I have the () braces in my codes. I'll edit the question. I totally spaced out putting the question. I have it in my codes, at first load it is working, but when the ajax is called, it won't init the Grid again. @billy Commented Feb 19, 2016 at 19:02

4 Answers 4

3
var Grid = (function() {
   function init( config ) { ... }
     .
     .
     .
   function addItems( $items ) { ... }

   // explicitly return the functions, or properties you want to be attached to `Grid`
   return {
       init: init,
       .
       .
       .
       addItems: addItems
   }
// assuming you want this to be immediately invoked function expression
})();
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, i forget to add the part for the return.
I have the return in my var grid, edited the question.
0

You defined init inside of the scope of the iife, but do not add it as a function of Grid.

you should do

Grid.init = function(){}

Comments

0

try it like this

var Grid = {
   init  : function( config ) { ... }
     .
     .
     .
   addItems : function( $items ) { ... }
}

you should define init as a property inside Grid and the value of that property is the return of the function, hope it helped

Comments

0

I tried to replicate your case there : JSFiddle

The Grid :

var Grid = (function() {
  function init(config) {
    console.log('--- INIT')
  }
  
  function addItems($items) {
    console.log('--- ADD ITEMS')
  }
  
  return { init: init, addItems: addItems };
})();

Then the function with the Ajax Call :

function updateItems() {
  jQuery.ajax({
      type: 'get',
      url:  ajaxURL,
      dataType: 'json',
      success: function(response) {
         Grid.init();
         Grid.addItems();
      }
  });
}

And I call this function :

updateItems();

Everything is fine for me. The console outputs :

--- INIT

--- ADD ITEMS

Can you spot a difference with your code ?

Are you sure Grid is global or in the same scope as updateItems ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.