2015-03-02 59 views
1

我正在编写一个插件从我的公司内部API中提取列表,除了某些原因,它不允许我链接其他方法。链接不工作jQuery插件

(function($) { 

    $.fn.addListings = function(options){ 
     var defaults = { 
      listingCount: 25, 
      pageNumber: 1, 
      customTemp: "<div class='listing'>\ 
      <img src='${IDXPhotoRef}'/>\ 
      <div class='address'>${Address}</div>\ 
      <div class='beds'> Beds: ${BedRooms}</div>\ 
      <div class='baths'>Baths: ${BathRooms}</div>\ 
      <div class='price'>Price: $${PriceFormatted}</div>\ 
      </div>", 
      after: function(){} 
     } 

     var settings = $.extend({}, defaults, options); 

     $.ajax({ 
      type: 'GET', 
      // url: '/api/listings/?featuredlistings=1&pagesize=' + settings.listingCount + '&pagenumber=' + settings.pageNumber + '', 
      url:'data.json', 
      contentType: 'text/plain', 
      crossDomain: true, 
      context: $(this) 
      }) 
     .done(function(data) { 
      $.template("customTemp", settings.customTemp); 
      var arrData = $.map(data[0], function(el) { return el; }); 
      for(i=0; i<arrData.length; i++){ 
       $.tmpl("customTemp", arrData[i]).appendTo(this); 
      } 

      }) 
     .always(function(){ 
      settings.after(); 
      }); 

    }; 

    return this; 

    }(jQuery)); 

https://github.com/cjthedizzy/jquery.addListingsJS/blob/master/jquery.addListings.js

+0

这里是错误的控制台吐出 遗漏的类型错误:无法读取属性“addClass”的undefined – ChrisJ 2015-03-02 17:23:58

回答

1

return this需要被放置在$.fn.addListings块内:

(function($) { 
    $.fn.addListings = function(options){ 
     // var defaults = { 
     // ... rest of the code... 

     return this; 
    }; 
}(jQuery)); 
+0

那真是令人尴尬。谢谢,有时你周一需要的是第二组眼睛。 – ChrisJ 2015-03-02 17:31:41