2010-10-18 19 views
0

我正在为我的网站使用我刚刚制作的搜索api制作一个小部件。简而言之,我想检索最后的帖子或最受好评的帖子。我必须为此制定标签。JQuery:如何使用由此插件的另一个函数生成的<a href>调用内部插件函数?

默认情况下,在加载时,我在最近的帖子。如果我点击顶帖子,我想调用一个函数在我的插件中修改一个var,告诉我的搜索API以正确的顺序检索数据。

我不知道我是不是很清楚。请,看看我的(简化)代码:

jQuery.fn.Widget=function(id_place,id_event,options){ 

// here, i tell that by default, you have to load latest posts 
    var defaults = { 
     launch : '1', // in seconds 
     max : '10', // nmber posts to retrieve 
     sort : 'last' 
    }; 
    var options = $.extend({}, defaults, options); 


// here the function I'd like to call to change the options.sort used to call the search API 
    var change_tab = function(sort) { 
     switch(sort){ 
      case 'last' : active[0] = ' class="active"'; break; 
      case 'top' : active[1] = ' class="active"'; break; 
      case 'info' : active[2] = ' class="active"'; break; 
         default: break; 
     } 
     options.sort = sort; 
    } 

// here, I try to call the change_tab function above, inside my plugin 
    $(".sort_top").click(function(){ 
     Widget(id_place,id_event,options).change_tab('top'); 
    }) ; 

// here, an extract of me tab menu of my widget. With the link to change sorting method 
    $("#myWidget").html('<a href="javascript:;" class="sort_top"><li'+active[1]+'>Top Questions</li>'); 


// here, I retrieve data an arrange it 
    jsonp_callback = function(data) { 
     if (data['results'].length > 0) { 
      // do something 
     } 
    } 

// here, I call my search API, with the param sort to retrieve data by time or popularity 
    widget_search = function() { 

     $.ajax({ 
      dataType: 'jsonp', 
      data: 'mydata&callback=?', 
      jsonpCallback: 'jsonp_callback', 
      url: 'http://myserver.com/search.php', 
      success: function() { 
      }, 
     }); 
    } 

// call the getJSON and refresh function 
    setTimeout("widget_search()",1000*options.launch); 
    var auto_refresh = setInterval(function() { widget_search(); }, 1000*options.refresh); 

}; 

回答

0

应传递一个变量上的数据一样简单:第Ajax查询,它将作为GET传递给您的数据源(默认情况下)您只需根据此变量调整查询以返回适当的数据,然后将其显示在您选择的div上,因为您已经似乎要做。

相关问题