2010-12-15 6 views
0

Je continue d'essayer d'appeler la fonction getMonthDay. Je passe un paramètre pubDate et continue d'obtenir l'erreur - Break on ErrorXCopyDisableContinue this.getMonthDay n'est pas une fonctionaide avec la syntaxe littérale - pas une fonction

Des idées?

$(document).ready(function(){ 
    alert = console.log; 


    var ns = { 

     init : function(){ 
      $.ajax({ 
       url: '/calendar/RSSSyndicator.aspx?type=N&number=15&category=8-0%2c4-0%2c6-0%2c10-0%2c7-0%2c17-0%2c16-0%2c9-0%2c5-0%2c3-0%2c2-0&department=3&numdays=31&ics=Y&rsstitle=Annandale+-+Event+Listing&rssid=11', 
       success: this.loaded     
      }); 
     }, 

     loaded: function(data){ 

      // Find item from the RSS document and iterate over reach one. 
      $(data).find('item').each(function(i, value){ 

       // Set the title and get rid of all chars including and between() 
       var title = $(this).find('title').text().replace(/\w+\s+\(.*?\)/, ""); 

       var link = $(this).find('link').text(); 
       var pubDate = $(this).find('pubDate').text(); 
       alert(title); 
       alert(link); 
       alert(pubDate); 

       test = this.getMonthDay(pubDate); 


       $('#events').append("<p>" + title + "<br/>" + link + "</p>"); 

      }); 

     // var t = items[0].getElementsByTagName('title'); 
     // alert(t[0].firstChild.nodeValue); 

     }, 



     getMonthDay : function(pubDate){ 

      var d = new Date(pubDate); 

      d.month = d.getMonth(); 
      var months = ["January", "February", "March", "Thursday", "Friday", "Saturday", "Sunday"]; 
      var month = months[d.month] 
      var newMonthDay = month + " " + d.getDay(); 

      return newMonthDay; 


     } 

    } 



    ns.init(); 




}); 
+0

J'appelle correctement la méthode? test = this.getMonthDay (pubDate); – steve

+0

Non. C'est l'erreur. Voir ma réponse ci-dessous. – VoteyDisciple

Répondre

0

Vous appelez this.getMonthDay à partir du rappel vous passez à .each(). À ce stade, this est un contexte complètement différent. Au lieu de cela, envisager quelque chose comme:

loaded: function(data){ 

    var ns = this; 

    // Find item from the RSS document and iterate over reach one. 
    $(data).find('item').each(function(i, value){ 

     // Set the title and get rid of all chars including and between() 
     var title = $(this).find('title').text().replace(/\w+\s+\(.*?\)/, ""); 

     var link = $(this).find('link').text(); 
     var pubDate = $(this).find('pubDate').text(); 
     alert(title); 
     alert(link); 
     alert(pubDate); 

     test = ns.getMonthDay(pubDate); 


     $('#events').append("<p>" + title + "<br/>" + link + "</p>"); 

    }); 

// var t = items[0].getElementsByTagName('title'); 
// alert(t[0].firstChild.nodeValue); 

}, 

Notez la création d'un alias (ns) pour this, qui peut alors continuer à se référer à cet objet, peu importe quels objets/contextes pourraient entrer en jeu plus tard.

+0

Je viens de faire la mise à jour pour le ns pour cela. Je suis gettign - ns.getMonthDay n'est pas une fonction – steve

+0

@steve - ce n'est que la moitié du problème, alors que ce n'est pas tout ce dont vous avez besoin, 'this' est perdu avant ... voir ma réponse pour une explication :) –

+0

Je l'ai juste fait travailler Nick et Votey. Vous êtes tous les deux des rockstars. Btw, auriez-vous utilisé la syntaxe littérale pour ce que j'essaie d'accomplir? Je suis à peu près un newb en ce moment. – steve

1

Il y a en réalité 2 problèmes ici. Le plus immédiat/local est que this fait référence à cet élément <item> vous êtes dans la boucle .each(), nous pouvons résoudre ce problème en tenant une référence en dehors de this, comme ceci:

var self = this; 

Puis dans la fonction, l'utilisation self.getMonthDay(pubDate);. Deuxièmement, après cela est fixé ... même alors this n'est pas ce que vous voulez, il se réfère à l'objet options que vous avez passé à $.ajax(). Pour remédier à cela, vous avez besoin context option là pour maintenir this être ns aussi bien, comme ceci:

$(document).ready(function(){ 
    alert = console.log; 
    var ns = { 
     init : function(){ 
      $.ajax({ 
       url: '/calendar/RSSSyndicator.aspx?type=N&number=15&category=8-0%2c4-0%2c6-0%2c10-0%2c7-0%2c17-0%2c16-0%2c9-0%2c5-0%2c3-0%2c2-0&department=3&numdays=31&ics=Y&rsstitle=Annandale+-+Event+Listing&rssid=11', 
       context: this,   //add this! 
       success: this.loaded     
      }); 
     }, 
     loaded: function(data){ 
      var self = this;   //maintain a reference to this 
      $(data).find('item').each(function(i, value){ 
       var title = $(this).find('title').text().replace(/\w+\s+\(.*?\)/, ""); 
       var link = $(this).find('link').text(); 
       var pubDate = $(this).find('pubDate').text(); 
       var test = self.getMonthDay(pubDate); 
       $('#events').append("<p>" + title + "<br/>" + link + "</p>"); 
      }); 
     }, 
     getMonthDay : function(pubDate){ 
      var d = new Date(pubDate); 
      d.month = d.getMonth(); 
      var months = ["January", "February", "March", "Thursday", "Friday", "Saturday", "Sunday"]; 
      var month = months[d.month] 
      var newMonthDay = month + " " + d.getDay(); 
      return newMonthDay; 
     } 
    } 
    ns.init(); 
});