How to catch error when call ajax request to server

Hi all, i using ajaxSource to get data from server,
var
kind = require('enyo/kind'),
Model = require('enyo/Model'),
AjaxSource = require('enyo/AjaxSource');

new AjaxSource({name: 'ajax'});

var MycontactCollection = kind({
name: 'MyContactCollection',
kind: Collection,
options: {parse: true},
source: 'ajax',
url: 'http://myservice.com/users'
parse: function (data) { // incoming data contains {status:..., result:...}
return data.result; // returned data contains {[{user_id:..., name:...}, {...}]}
}
});

I see errored function in enyo.Collection Document but i don't know use it in my collection.
public errored(action, opts, res, source)→ undefined
How to catch error when server error.
Thank you very much.

Comments

  • try sending in a error function in your options block, like:
    options: {parse:true, 
    success: enyo.bind(this, this.successHandler),
    error: enyo.bind(this, this.errorHandler)}
    (error might need be errored)
  • Thanks @psarin, i solved by set error: true in options property and overide errored function and check errorCode in my view to catch error.

    options:{
    parse:true,
    error: true
    },
    errored:function(){
    console.log('loi goi api');
    this.set('errorCode',true);
    },


  • Hmm, sending in an error handler in options should work, and wouldn't require you to overload the builtin error function which updates the internal STATUS.

    Collection.js line 1187

    // we need to check to see if there is an options handler for this error
    if (opts && opts.error) opts.error(this, action, opts, res, source);
  • @psarin I see errored function in collection.js
    errored: function (action, opts, res, source) {..
    But i don't know using it in my custom collection, because i see at least 2 params action, opts past this function
Sign In or Register to comment.