问题描述
我使用Rails 4,引导和砌体。我有以下的code工作的jQuery砌体安排我的div,在application.js中:
I'm using Rails 4, Bootstrap and Masonry. I have the following code working for jQuery Masonry to arrange my divs, in application.js:
$(function(){
$('#pins').masonry({
itemSelector: '.box',
isFitWidth: true
});
});
var masonryUpdate = function() {
setTimeout(function() {
$('#pins').masonry();
}, 200);
}
$(document).on('click', masonryUpdate);
$(document).ajaxComplete(masonryUpdate);
它的工作原理,否则,但当我尝试删除与AJAX项目,砌体不会更新。这是我的destroy.js:
It works otherwise, but when I try to delete an item with AJAX, Masonry doesn't update. Here is my destroy.js:
$('.deleting').bind('ajax:success', function() {
$(this).closest('.poista').fadeOut();
});
我怎么能强迫砌体重新加载,高于code例子后?出于某种原因,.ajaxComplete(masonryUpdate)就不会被触发?
How could I force Masonry to reload, after the code example above? For some reason .ajaxComplete(masonryUpdate) is not triggered?
推荐答案
从jQuery 文档上ajaxComplete它好像它并不在一个给定的参数执行函数调用,而是调用处理函数,当Ajax请求完成。
From the jQuery Documentation on ajaxComplete it seems as though it doesn't perform a function call on a given argument but instead calls a handler function when the Ajax requests complete.
处理器 类型:功能(事件事件,jqXHR jqXHR,PlainObject ajaxOptions) 的功能将被调用。
您最好的选择是使用一个匿名函数来调用 masonryUpdate
。
Your best bet would be to use an anonymous function to call masonryUpdate
.
$(document).ajaxComplete(function(event, xhr, settings) {
masonryUpdate();
};
修改
这可能会更好缓存您的砌体规范的一个变量。
It might be better to cache your masonry spec in a variable.
var mas = $('#pins').masonry({
itemSelector: '.box',
isFitWidth: true
});
然后就可以调用砖石该变量
Then you can call masonry on that variable
var masonryUpdate = function() {
setTimeout(function() {
mas.masonry('reload');
}, 200);
}
这篇关于.ajaxComplete删除与AJAX项目后,就不会被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!