ember.js - Ember Data model's errors property (DS.Errors) not populating -


i'm using ember data , can't seem model's 'errors' property populate error messages rest api. i'm pretty following example @ guide:

http://emberjs.com/api/data/classes/ds.errors.html

my app looks this:

    window.app = ember.application.create();      app.user = ds.model.extend({         username: ds.attr('string'),         email: ds.attr('string')     });      app.applicationroute = ember.route.extend({         model: function () {             return this.store.createrecord('user', {                 username: 'mike',                 email: 'invalidemail'             });         },          actions: {             save: function () {                 this.modelfor(this.routename).save();             }         }     }); 

and api returns this:

http/1.1 400 bad request content-type: application/json; charset=utf-8 content-length: 125  {   "errors": {     "username": ["username taken!"],     "email": ["email invalid."]   } } 

after call save() on model, here see on user model:

user.get('iserror') // true user.get('errors.messages') // [] 

even though model registering iserror property correctly, can't seem error messages populate. how can work? i'm working on latest beta build of ember data version 1.0.0-beta.8.2a68c63a

the docs lacking in area, errors aren't populated unless you're using active model adapter.

here's example of working, check out ember: error.messages not show server errors on save same thing

http://jsbin.com/motuvaye/24/edit

you can implement on restadapter overriding ajaxerror , copying how active model adapter it.

app.applicationadapter = ds.restadapter.extend({    ajaxerror: function(jqxhr) {       var error = this._super(jqxhr);      if (jqxhr && jqxhr.status === 422) {       var response = ember.$.parsejson(jqxhr.responsetext),           errors = {};        if (response.errors !== undefined) {         var jsonerrors = response.errors;          ember.enumerableutils.foreach(ember.keys(jsonerrors), function(key) {            errors[ember.string.camelize(key)] = jsonerrors[key];         });       }       return new ds.invaliderror(errors);     } else {       return error;     }   } }); 

http://jsbin.com/motuvaye/27/edit

https://github.com/emberjs/data/blob/v1.0.0-beta.8/packages/activemodel-adapter/lib/system/active_model_adapter.js#l102


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -