弹出内容的JSON数据,有SCOPE的麻烦

时间:2013-10-10 06:54:07

标签: javascript ajax json

谢谢 peernohell 它让我更进一步。 无论如何,从getContent方法返回时, var 内容仍未定义 我知道这些是范围问题,所以我说我提出了一个错误的问题。 如何在这种情况下如下处理范围:

!function ($) {

  "use strict"; // jshint ;_;


 /* POPOVER PUBLIC CLASS DEFINITION
  * =============================== */

  var Popover = function (element, options) {
    this.init('popover', element, options)
  }


  /* NOTE: POPOVER EXTENDS BOOTSTRAP-TOOLTIP.js
     ========================================== */

  Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype, {

    constructor: Popover

  , setContent: function () {    
      var $tip = this.tip()
        , title = this.getTitle()
        , content = this.getContent()

      $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
      $tip.find('.popover-content')[this.options.html ? 'html' : 'text'](content)

      $tip.removeClass('fade top bottom left right in')
    }

  , hasContent: function () {
      return this.getTitle() || this.getContent()
    }

  , getUrl: function (){
    var el = this.$element;
    var url = 'http://www.google.com/finance/info?infotype=infoquoteall&q=' + 
               this.getId() + 
              '&callback=?'
              ;
    return url;
  }  
  , getId : function(){
    var tooltipId
    , id = this.$element
    , o = this.options

    tooltipId =  id.attr('data-load');

      return tooltipId;
    }
  , getData: function (callback) {
      var url = this.getUrl();
      var result = [];

      $.ajax({
          url: url,
          dataType: "json",          
          success: function (data) {
              $.each(data, function (entryIndex, entry) {
                  result.push(entry.t);

              });
              callback(result);                        
          },
          error: function () {
              calllback();
          }
      });
  }

  , getContent: function() {
      var content
        , $e = this.$element
        , o = this.options

      content = this.getData(function(result){
                    var info = result.toString();
                    return info;
      });

      return content
    }

  , tip: function () {
      if (!this.$tip) {
        this.$tip = $(this.options.template)
      }
      return this.$tip
    }

  , destroy: function () {
      this.hide().$element.off('.' + this.type).removeData(this.type)
    }

  })


 /* POPOVER PLUGIN DEFINITION
  * ======================= */

  var old = $.fn.popover

  $.fn.popover = function (option) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('popover')
        , options = typeof option == 'object' && option
      if (!data) $this.data('popover', (data = new Popover(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }

  $.fn.popover.Constructor = Popover

  $.fn.popover.defaults = $.extend({} , $.fn.tooltip.defaults, {
    placement: 'top'
  , trigger: 'hover'
  , content: ''
  , template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
  })


 /* POPOVER NO CONFLICT
  * =================== */

  $.fn.popover.noConflict = function () {
    $.fn.popover = old
    return this
  }

}(window.jQuery);

1 个答案:

答案 0 :(得分:1)

getData是一个异步函数,你必须给它一个回调,所以更新它的代码以使用像这样的回调

getData: function (callback) {
      var url = this.getUrl();
      var result = [];

      $.ajax({
          url: url,
          dataType: "json",          
          success: function (data) {
              $.each(data, function (entryIndex, entry) {
                  result.push(entry.t);

              });
              callback(result);
              return result;

          },
          error: function () {calllback();}
      });
  }

以及一些你想要getData的地方

this.getData(function (result) { 
    if (!result) { 
         //error case
    }
    // do some things with result
});