2012-09-03 40 views
0

我建立jQuery插件:jQuery的這個範圍內插件

 (function($){ 

      var methods = { 
       init : function(options) { 
        $.fn.queryBuilder = $.extend($.fn.queryBuilder, options); 

        return this.each(function(){ 

         methods.getValues(); 

         var $this = $(this), 
         data = $this.data('queryBuilder'), 
         url = '/'+methods.createURL.call(); 

         if (! data) { 
          $(this).data('queryBuilder', { 
           target : $this, 
           url  : url 
          }); 
         } 
        }); 
       }, 
       getURL:function(){ 
        alert $(this); 
       } 
    } 
$.fn.queryBuilder = function(method) { 
     if (methods[method]) { 
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
     } else if (typeof method === 'object' || ! method) { 
      return methods.init.apply(this, arguments); 
     } else { 
      $.error('Method ' + method + ' does not exist on jQuery.tooltip'); 
     } 
    }; 
}) 

當的GetValues()方法被調用時,警報顯示未定義。如何使用範圍調用getValues到我的插件綁定的對象?

回答

0

代碼中沒有getValues函數,我假設你的意思是getURL。您可以通過使用.call[MDN].apply[MDN]設置this明確:

methods.getURL.call(this); 

這是一樣的,你在

return methods.init.apply(this, arguments);