Michael Oakley
Michael Oakley

Reputation: 383

Javascript callback function not executing

I have a callback function that isn't executing. I suspect it's being treated as a string, but I'm not sure. The code is below. Also, here's a simplified jsFiddle with more details: http://jsfiddle.net/oakley808/sH5XE/3/

Basically it just iterates a for loop, using settings from an object. The final line config.feeds[i].cb is what fails. Ideas anyone?

// the callback function
function rssdone(){
    $('#cbBlock').append('did some callback stuff<br>');    
}

// the settings for the loop below
var config = {
  "feeds": [
    {
        "container": "#block1",
        "url":"http://apps1.eere.energy.gov/news/rss/program.cfm?topic=1010",
        "limit":"4",
        "layoutTemplate": "<ol type='1'>{entries}</ol>",
        "entryTemplate": "<li>{title}</li>",
        "cb":"rssdone"
    },
    {
        "container": "#block2",
        "url":"http://apps1.eere.energy.gov/news/rss/financial_opps_solar.cfm",
        "limit":"2",
        "layoutTemplate": "<ol type='A'>{entries}</ol>",
        "entryTemplate": "<li>{title}</li>",
        "cb":"rssdone"
    }
  ]
}       

// the logic    
for( var i=0; i < config.feeds.length; i+=1 ) {
  $( config.feeds[i].container ).rss(
    config.feeds[i].url,
    {
        limit:          config.feeds[i].limit,
        layoutTemplate: config.feeds[i].layoutTemplate,
        entryTemplate:  config.feeds[i].entryTemplate
    },
    // this fails to use the callback for some reason
    config.feeds[i].cb

    // use this instead and it works!
    // rssdone
  );

}

Upvotes: 1

Views: 113

Answers (4)

laconbass
laconbass

Reputation: 17805

Solution 1

This solution is aplicable only if the config object can be a true js Object.

Fiddle with the solution 1

Solution 2

If you are going to retrieve your config object throught AJAX and it will be a JSON object, is not possible to reference the callback function from it. One possible aproach in this case is to store the callback function on the global scope (window object) and use the variable name to reference the function:

// the callback function
window.rssdone = function(){
    $('#cbBlock').append('did some callback stuff<br>');    
};

// the settings for the loop below
var config = {
  "feeds": [
    {
      ...
      "cb":"rssdone"
    },
    {
      ...
      "cb":"rssdone"
    }
  ]
}       ;

// the logic    
for( var i=0; i < config.feeds.length; i+=1 ) {
  $( config.feeds[i].container ).rss(
    config.feeds[i].url,
    {
      limit:          config.feeds[i].limit,
      layoutTemplate: config.feeds[i].layoutTemplate,
      entryTemplate:  config.feeds[i].entryTemplate
    },
    // reference to the callback on the global scope
    window[ config.feeds[i].cb ];
  );
}

Fiddle with the solition 2

Upvotes: 0

Chase Wilson
Chase Wilson

Reputation: 1487

You're correct in that you're only passing a string when this .rss plugin is expecting a function. You can't fire a string.

If you wanted to fire a function that you're referencing as a string, you're going to need to reference the function as a method of some scope object.

For instance:

var obj = {
  something: function () {
    alert('here')
  } 
}

function executeCallback (reference, scope) {
   scope[reference]()
}

executeCallback('something', obj)

See here: http://jsfiddle.net/sH5XE/4/

Upvotes: 0

Travis J
Travis J

Reputation: 82267

jsFiddle Demo

That is because it is just a string. What you could do is just make an object which held your rss function and then use the string accessor to call the function

var cbHolder = {};
cbHolder.rssdone = function(){
 $('#cbBlock').append('did some callback     stuff<br>');   
};

and then

cbHolder[config.feeds[i].cb]

Upvotes: 1

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

Yes, you're storing your config object as json instead of a js object. The quotes cause it to be treated as a string. Remove the quotes from around the "rssdone" references and it should work:

var config = {
  "feeds": [
    {
        "container": "#block1",
        "url":"http://apps1.eere.energy.gov/news/rss/program.cfm?topic=1010",
        "limit":"4",
        "layoutTemplate": "<ol type='1'>{entries}</ol>",
        "entryTemplate": "<li>{title}</li>",
        "cb":rssdone
    },
    {
        "container": "#block2",
        "url":"http://apps1.eere.energy.gov/news/rss/financial_opps_solar.cfm",
        "limit":"2",
        "layoutTemplate": "<ol type='A'>{entries}</ol>",
        "entryTemplate": "<li>{title}</li>",
        "cb":rssdone
    }
  ]
}    

Upvotes: 2

Related Questions