Skip to content

Instantly share code, notes, and snippets.

@irfan
Created November 28, 2012 14:22
Show Gist options
  • Save irfan/4161611 to your computer and use it in GitHub Desktop.
Save irfan/4161611 to your computer and use it in GitHub Desktop.
wrong data structure is wrong
/*
* data structure:
* { status: 0, data:{}}
* { status: 0, data:{}, message: "you did something" }
* { status: 1, data:{}, message: "you did something" }
* { status: 1, data:{}}
* { status: 2, data:{response:{data:{form:{global:'something going wrong'}}}}}
* { status: 2, data:{response:{data:{form:{global:'error'}}}}, message: "you did something" }
* { status: 2, data:{response:{data:{form:{global:'error'}}}}, message: "you did something" }
* { status: 2, data:{response:{data:{form:{global:'error'}}}}, message: "you did something" }
* { status: 1404, data:{}, message: "you did something" }
*
*/
var handler = function(response, status, xhr){
var errors = {}, i, l;
errors['globalErrors'] = [];
errors['formErrors'] = [];
errors['fieldErrors'] = [];
if (that.checkResponse(xhr) === 'json') {
// even status is 0 may data could not found
if(response.status == '0' && response.data.length < 1 ){
return;
}
// status is not 0 then we have error
if(response.status != '0') {
// status is 1 then we have kind of global logic error
// status is bigger than 1000 then we have http error code
if(response.status == 1 || response.status > 1000) {
that.get('notify').show({
type: 'error',
content: response.message
});
return;
}
// form errors
if(response.status == '2' ) {
// I dont know what we'll do this. It is not in the design.
if(response.data.form.global !== undefined) {
errors.formErrors = response.data.form.global; // always array
}
l = response.data.form.fieldset.length;
// if there are no any field dont use the for
if ( l > 0 ) {
// if there are any fields
for ( i = 0; i < 0; i += 1 ) {
// if there are any field has error
if (response.data.form.fieldset[i].error) {
errors['fieldErrors'].push(response.data.form.fieldset[i].error);
}
}
}
}
}
// finally data valid, return it
if (response.status == '0' && response.data.length > 0) {
return response;
}
}
// it is html response, not json
else {
return response;
}
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment