How to catch error when call ajax request to server

Hi all, i using ajaxSource to get data from server,
var<br /> kind = require('enyo/kind'),<br /> Model = require('enyo/Model'),<br /> AjaxSource = require('enyo/AjaxSource');<br /><br /> new AjaxSource({name: 'ajax'});<br /><br />var MycontactCollection = kind({<br /> name: 'MyContactCollection',<br /> kind: Collection,<br /> options: {parse: true},<br /> source: 'ajax',<br /> url: 'http://myservice.com/users'<br /> parse: function (data) { // incoming data contains {status:..., result:...}<br /> return data.result; // returned data contains {[{user_id:..., name:...}, {...}]}<br /> }<br /> });<br />
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, <br />success: enyo.bind(this, this.successHandler), <br />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:{<br /> parse:true,<br /> error: true<br /> },<br /> errored:function(){<br /> console.log('loi goi api');<br /> this.set('errorCode',true);<br /> },

  • 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
    <br />			// we need to check to see if there is an options handler for this error<br />			if (opts && opts.error) opts.error(this, action, opts, res, source);<br />
  • @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.