【发布时间】:2014-08-30 08:01:41
【问题描述】:
我正在开发一个加载 AJAX 的购物应用程序,其布局为 Masonry。
我使用循环来加载和显示产品:
function get_next_entry() {
$data = next_entries(); //get the ids of the next 6 products
$.ajax({
type: 'POST',
url: $url,
data: $data,
cache: true,
success: function(data) {
var xml = $(data);
xml.find('entry').each(function(){
var el = $($(this).text());
el.imagesLoaded(function() {
$collection.append( el ).masonry( 'appended', el, true );
el = set_visibility( el );
});
});
if ($resync) {
$collection.masonry();
}
get_next_entry();
},
dataType: 'xml',
async:true
});
}
$data 包含一个产品 ID 数组。每个产品的 HTML 都由其 id 缓存,因此无需 MySQL 查询即可检索它们,以减少我的数据库服务器上的负载,并且可以更快地提供服务。
我有一个用于切换可见性的产品的 jQuery 过滤系统。
选择过滤器后,我只需要加载该过滤器中的产品。当过滤器被取消选择时,我需要继续加载整个目录。
我不想对多个 MySQL 查询执行此操作,因为每个用户会有很多此类查询(我的循环一次加载 6 个产品,因此每个循环周期的查询会太多)。
我当前的解决方案是通过 AJAX 将整个目录列表作为ids 的 XML 列表获取,并将过滤器作为属性(这样我可以轻松地使用 jQuery 而不是 JSON 进行过滤),然后在加载产品时从该列表中删除产品:
<data>
<product category="category">id</product>
<product category="category">id</product>
<product category="category">id</product>
...
</data>
有没有更好的方法来做到这一点?
【问题讨论】:
-
你好,目前这种方法是否花费了太多时间?
-
嗨,不是真的,XML 列表也被缓存了,所以响应速度非常快,在检索列表时加载有点暂停,但还不错,我只是对可能的人的想法感兴趣有更好的解决方案。
标签: javascript php jquery mysql ajax