Ajax之外的JQuery调用函数?

时间:2022-10-08 08:33:55

I currently have created a pagination Javascript class that will automatically paginate comments when you get more than 10 to a page. This works fine, but I'd like to set some CSS properties only if there are more than 10 comments on the page (add space for the buttons to switch between pages). This COULD be done in the ajax success, but I want to keep the paginate.js so that its usable by the rest of my app (other parts of the app do not need padding added), and not with a bunch of if's and else's to add padding only in one scenario. So, is there any way that I can bind an event to the success of the ajax function externally? For example, my current page includes these files:

我目前已经创建了一个分页Javascript类,当您超过10个页面时,它将自动分页注释。这工作得很好,但是我想设置一些CSS属性,只有当页面上有超过10条评论时(为页面之间的按钮添加空间)。这可以在ajax成功中完成,但是我想保留这个分页。这样它就可以被我的应用程序的其他部分使用(应用程序的其他部分不需要添加填充),而不需要在一个场景中添加很多if和else的填充。那么,有什么方法可以将事件绑定到ajax功能的成功上呢?例如,我的当前页面包含以下文件:

pagination.js
detail.js

Pagination runs the ajax call by passing it the following arguments from detail.js:

Pagination通过传递以下来自细节的参数来运行ajax调用:

  p = new paginate({
    table_id: 't1',
    pageselect_max: 7,
    rpp_options: [5,10,25,50],
    rpp: 5,
    columns: [''],
    wildcard: stock_symbol,
    url: 'trader/stocks/ajax/',
    action: 'paginate_comments',
    noresults: false
  });

This is what the ajax looks like inside pagination.js:

这就是页面内的ajax:

  this.get_results = function() {
    oThis = this; // Needed because inside the success function 'this' points to something else
    $.ajax({
      type: "POST",
      url: this.url,
      data: "action="+this.action+"&rpp="+this.rpp+"&page="+this.page+"&sortBy="+this.sortBy+"&sortDir="+this.sortDir+"&wildcard="+this.wildcard,
      dataType: "json",
      success: function(json) {
        if (json.error) {
          alert(json.error);
        }
        else {
          var data = json.data;
          oThis.count = data.count;
          $("#p_count").text(oThis.count);
          var resText = oThis.count == 1 ? "result" : "results";
          $("#p_results").text(resText);
          if (!data.disable_cufon) {
            Cufon.replace('h2');
            Cufon.replace('h1');
          }
          oThis.results = data.results;
          oThis.update_pageselect();
          oThis.display_results();
          oThis.do_rpp();
        }
      },
      error: function(error) {
        alert(error.responseText);
      }
    });
  }

I want to be able to reference the success in detail.js and run this:

我想要能够详细地提及成功。js和运行:

if(p.maxPages > 1){
  $("#commentscontainer .cmtgocontainer").css("margin-top","48px");
}

I know this can be accomplished by changing asynchronous property, but that seems like bad practice. Any better methods of doing this without altering pagination.js?

我知道这可以通过更改异步属性来实现,但这似乎是一种糟糕的做法。有没有更好的方法可以在不改变page .js的情况下实现这一点?

1 个解决方案

#1


3  

You could make your own callback function and call it in pagination.js:

你可以创建自己的回调函数并在pagins .js中调用:

For example:

例如:

//In your success callback:
if (typeof oThis.onPaginateComplete === "function")
    oThis.onPaginateComplete();

You could then write the following in detail.js:

然后,您可以在detail.js中编写以下内容:

p.onPaginateComplete = function() {
    if (this.maxPages > 1) {
        $("#commentscontainer .cmtgocontainer").css("margin-top","48px");
    }
};

#1


3  

You could make your own callback function and call it in pagination.js:

你可以创建自己的回调函数并在pagins .js中调用:

For example:

例如:

//In your success callback:
if (typeof oThis.onPaginateComplete === "function")
    oThis.onPaginateComplete();

You could then write the following in detail.js:

然后,您可以在detail.js中编写以下内容:

p.onPaginateComplete = function() {
    if (this.maxPages > 1) {
        $("#commentscontainer .cmtgocontainer").css("margin-top","48px");
    }
};