要加载我的页面,我必须按特定顺序制作几个AJAX POSTS,我通过使用.done()函数实现了这一点:
import android.graphics.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class MaterialColorPalette {
public static final int RED_500 = 0xFFF44336;
public static final int PINK_500 = 0xFFE91E63;
public static final int PURPLE_500 = 0xFF9C27B0;
public static final int DEEP_PURPLE_500 = 0xFF673AB7;
public static final int INDIGO_500 = 0xFF3F51B5;
public static final int BLUE_500 = 0xFF2196F3;
public static final int LIGHT_BLUE_500 = 0xFF03A9F4;
public static final int CYAN_500 = 0xFF00BCD4;
public static final int TEAL_500 = 0xFF009688;
public static final int GREEN_500 = 0xFF4CAF50;
public static final int LIGHT_GREEN_500 = 0xFF8BC34A;
public static final int LIME_500 = 0xFFCDDC39;
public static final int YELLOW_500 = 0xFFFFEB3B;
public static final int AMBER_500 = 0xFFFFC107;
public static final int ORANGE_500 = 0xFFFF9800;
public static final int DEEP_ORANGE_500 = 0xFFFF5722;
public static final int BROWN_500 = 0xFF795548;
public static final int GREY_500 = 0xFF9E9E9E;
public static final int BLUE_GREY_500 = 0xFF607D8B;
private static final List<MaterialColorPalette> MATERIAL_PALETTES;
private static final Random RANDOM = new Random();
static {
MATERIAL_PALETTES = new ArrayList<>();
MATERIAL_PALETTES.add(new MaterialColorPalette(RED_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(PINK_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(PURPLE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(DEEP_PURPLE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(INDIGO_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(BLUE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(LIGHT_BLUE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(CYAN_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(TEAL_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(GREEN_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(LIGHT_GREEN_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(LIME_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(YELLOW_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(AMBER_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(ORANGE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(DEEP_ORANGE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(BROWN_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(GREY_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(BLUE_GREY_500));
}
public static int getRandomColor(String key) {
return MATERIAL_PALETTES.get(RANDOM.nextInt(MATERIAL_PALETTES.size())).getColor(key);
}
/**
* Lighten or darken a color
*
* @param color
* color value
* @param percent
* -1.0 to 1.0
* @return new shaded color
* @see #shadeColor(String, double)
*/
public static int shadeColor(int color, double percent) {
return shadeColor(String.format("#%06X", (0xFFFFFF & color)), percent); // ignores alpha channel
}
/**
* Lighten or darken a color
*
* @param color
* 7 character string representing the color.
* @param percent
* -1.0 to 1.0
* @return new shaded color
* @see #shadeColor(int, double)
*/
public static int shadeColor(String color, double percent) {
// based off http://stackoverflow.com/a/13542669/1048340
long f = Long.parseLong(color.substring(1), 16);
double t = percent < 0 ? 0 : 255;
double p = percent < 0 ? percent * -1 : percent;
long R = f >> 16;
long G = f >> 8 & 0x00FF;
long B = f & 0x0000FF;
int red = (int) (Math.round((t - R) * p) + R);
int green = (int) (Math.round((t - G) * p) + G);
int blue = (int) (Math.round((t - B) * p) + B);
return Color.rgb(red, green, blue);
}
private final HashMap<String, Integer> palette = new HashMap<>();
/**
* @param primary
* the 500 color
*/
public MaterialColorPalette(int primary) {
palette.put("50", shadeColor(primary, 0.9));
palette.put("100", shadeColor(primary, 0.7));
palette.put("200", shadeColor(primary, 0.5));
palette.put("300", shadeColor(primary, 0.333));
palette.put("400", shadeColor(primary, 0.166));
palette.put("500", primary);
palette.put("600", shadeColor(primary, -0.125));
palette.put("700", shadeColor(primary, -0.25));
palette.put("800", shadeColor(primary, -0.375));
palette.put("900", shadeColor(primary, -0.5));
palette.put("A100", shadeColor(primary, 0.7));
palette.put("A200", shadeColor(primary, 0.5));
palette.put("A400", shadeColor(primary, 0.166));
palette.put("A700", shadeColor(primary, -0.25));
}
public int getColor(String key) {
return palette.get(key);
}
public void putColor(String key, int color) {
palette.put(key, color);
}
}
现在我正在尝试实现错误处理,以便当检测到错误(由我或HTTP错误代码)加载停止并显示错误消息时,我无法弄清楚如何执行此操作:< / p>
$(document).ready(function(){
// Load Recipients & documents
$("#page").hide();
$("#loading").show();
// Specify the loading order
load_recipients().done(function(){
load_documents().done(function() {
$.when( initialize() ).then(function() {
var deferreds = [];
page_list.forEach(function(page_id, index) {
deferreds.push(load_fields(page_id));
});
$.when.apply(null, deferreds).done(function() {
$("#page").show();
$("#loading").hide();
});
});
});
});
});
到目前为止,我已尝试过e.preventDefault(),. stop()和stopPropogation(),但它们都没有阻止下一次发生AJAX POST。有没有人对我如何处理这个有任何建议?
答案 0 :(得分:0)
您可以创建自己的承诺,以便自己解决或拒绝。
function load_recipients() {
var promise = $.Deferred().promise();
$.ajax({
url: base_url + "recipients/get/",
type: "POST",
data: {
packets_id : packets_id
},
success: function(response_data) {
recipient_list = JSON.parse(response_data);
if(recipient_list.hasOwnProperty('error')) {
promise.reject();
} else {
promise.resolve();
}
},
error: function(jqXHR){
promise.reject();
}
});
return promise;
}
至于处理拒绝,我相信$ .when()。然后处理这个。
https://api.jquery.com/deferred.then/
deferred.then( doneFilter [, failFilter ] [, progressFilter ] )
doneFilter类型:Function()解析Deferred时调用的函数。
failFilter类型:Function()拒绝延迟时调用的可选函数。
答案 1 :(得分:0)
我不确定这是否是实现我想要做的最好或最优雅的方式,但它确实有效。我将成功检查移到了完成功能。
$(document).ready(function(){
// Load Recipients & documents
$("#page").hide();
$("#loading").show();
// Specify the loading order
load_recipients().done(function(data){
recipient_list = JSON.parse(data);
if(recipient_list.hasOwnProperty('error')) {
$("#alert-section .alert-danger").html("Please verify a recipient exists for this packet <a href='#'>Go Back</a>");
$("#alert-section .alert-danger").removeClass("hidden");
return false; // Prevents next AJAX POST
} else {
console.log("It's all good in the hood");
}
load_documents().done(function() {
$.when( initialize() ).then(function() {
var deferreds = [];
page_list.forEach(function(page_id, index) {
deferreds.push(load_fields(page_id));
});
$.when.apply(null, deferreds).done(function() {
$("#page").show();
$("#loading").hide();
});
});
});
});
});
function load_recipients() {
return $.ajax({
url: base_url + "recipients/get/",
type: "POST",
data: {
packets_id : packets_id
},
error: function(jqXHR){
// When this error is hit, all subsequent AJAX POSTs are stopped automatically
$("#alert-section .alert-danger").html("Please verify a recipient exists for this packet <a href='#'>Go Back</a>");
$("#alert-section .alert-danger").removeClass("hidden");
}
});
}