0
無限重裝我有我包括了自動重載頁面每個Ajax成功長JavaScript文件,但我得到了這個Ajax的停止功能
<script>
var latest;
var par;
var widgets;
var test;
var $randomId;
var gridster;
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
gridster = $(".gridster ul").gridster({
widget_margins: [10, 10],
widget_base_dimensions: [140, 140],
helper: 'clone',
resize: {
enabled: true
},
serialize_params: function($w, wgd) {
return {
id:$($w).attr('id'),
col: wgd.col,
row: wgd.row,
size_x:wgd.size_x,
size_y:wgd.size_y,
};
},
draggable: {
stop: function(event, ui) {
var positions = JSON.stringify(this.serialize());
var data_id = $(ui.$helper[0]).attr('id');
var size_x = $(ui.$helper[0]).attr('data-sizex');
var size_y = $(ui.$helper[0]).attr('data-sizey');
var data_row = $(ui.$helper[0]).attr('data-row');
var data_col = $(ui.$helper[0]).attr('data-col');
$.ajax({
url: "/partA-save-widgets-positions",
type: "POST",
data: {
'size_y': size_y,
'size_x': size_x,
'data_row': data_row,
'data_col': data_col,
'id' : data_id
},
dataType: "json",
error: function(log) {
console.log(log);
},
success: function(log) {
//console.log(log);
}
})
}
}
}).data('gridster');
$.getJSON('/partA-widgets-current-positions', function(data) {
if (data !=null) {
var id_name;
id_name = 'widgets';
id_name = id_name + data.id;
$.each(JSON.parse(data.positions), function(i, value) {
console.log(value);
$(id_name).attr({
'data-col': value.col,
'data-row': value.row,
'data-sizex': value.size_x,
'data-sizey': value.size_y
});
});
} else {
console.log("no data returned");
}
})
function Generator(){};
Generator.prototype.rand = Math.floor(Math.random() * 26) + Date.now();
Generator.prototype.getId = function() {
return this.rand++;
};
widgetTemplate = function($widgetTitle,$widgetContent){
idGen = new Generator();
$randomId = idGen.getId();
var html = $widgetContent;
return html;
},
addWidget = function($widgetType){
var mapAPI='<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d253304.652017724!2d125.31098975138514!3d7.25390594287568!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x32f96d9f519e327f%3A0xb53a24589f79c573!2sDavao+City%2C+Davao+del+Sur!5e0!3m2!1sen!2sph!4v1467344380693" width="200" height="150" frameborder="0" style="border:0" allowfullscreen></iframe>';
var costApi = 'https://api.del.icio.us/v1/';
widgets = {
'map': widgetTemplate('MAP',mapAPI),
'cost': widgetTemplate('COST','COST CONTENT'),
'revenue': widgetTemplate('REVENUE','REVENUE CONTENT'),
'traffic': widgetTemplate('TRAFFIC','TRAFFIC CONTENT'),
'admin': widgetTemplate('ADMIN','ADMIN CONTENT'),
'feedback': widgetTemplate('FEEDBACK','FEEDBACK CONTENT'),
'default': 'Error/Invalid Input'
};
}
$("ul.dropdown-menu li").click(function() {
par = $.trim($(this).find("span.widgets").text());
//var data_id = $(".random").closest('div.portlet').attr('data-id');
var style = $('.ui-resizable').attr('style');
idGen = new Generator();
var $randomId2 = idGen.getId();
console.log($randomId2);
switch(par) {
case "MAP": {
test = widgets.map;
break;
}
case "COST": {
test = widgets.cost;
break;
}
case "REVENUE": {
test = widgets.revenue;
break;
}
case "TRAFFIC": {
test = widgets.traffic;
break;
}
case "ADMIN": {
test = widget.admin;
break;
}
case "FEEDBACK": {
test = widget.feedback;
break;
}
default: {
console.log(par);
}
}
$.ajax({
url: '/partA-save',
type: "POST",
data: {
'test': test,
'widget': par,
'random_id': $randomId2,
'style': style
},
dataType: 'JSON',
}).success(function(data) {
console.log(data);
});
console.log(par);
console.log(test);
console.log($randomId2);
//return true;
});
//delete
$(".remove").click(function() {
var id = $(this).closest('div.portlet').attr('data-id');
$.ajax({
url: '/partA-delete',
type: "post",
data: {
'id': id
},
dataType: "JSON",
//async: false,
success: function(data) {
if (data.status == 200) {
setTimeout(function(){
location.reload();
},5000);
}
},
error: function(data) {
console.log(data);
}
});
});
//auto reload page if success
$(document).ajaxStop(function() {
window.location.reload();
});
});
</script>
這只是自動停止回調,但不會重新加載頁面例子裏面,如果結合其他功能被觸發 –
當您在成功消息中附加$(document).ajaxStop(fn)時,它會停止無限窗口重新加載,然後它應該在下一次ajax調用時觸發。 – Steve