I continue with learning of ember JS and I’m testing now ember data with UPDATING entities via simple REST API.
My problem is simple, I’ve got the following action which invokes my REST PUT method. My commodity is saved successfully via hibernate, server returns HttpStatus.OK, but Ember promise ALWAYS calls failure function…???
In the reason I don’t see any error, just my HttpStatus.OK and ready state = 4. Which is very confusing.
actions: {
save: function() {
var commodity = this.modelFor('commodity');
var post = this.store.update('commodity', commodity);
var self = this;
function transitionToPost(post) {
alert('Commodity updated successfully');
self.transitionToRoute('commodity');
}
function failure(reason) {
alert(reason);
}
post.save().then(transitionToPost).catch(failure);
}
}
I want to be able to catch specific HttpStatus codes from REST calls, do I have any chance with this in Ember data? Or do I need to implement this in the jquery?
It’s likely ember-data is throwing an exception which is triggering the error hook of the RSVP. In your failure callback console.error(reason.stack) to see the exception.
which is valid JSON.
Btw it seems that I’m gonna need to rewrite Ember Serializer, because by default it strips out the ID, which is very strange default behaviour.
To the problem, have you seen such error before? Most of the links suggests that the JSON is invalid, but
that’s not my problem here…
thanks in advance
T.
Edit: I’m obviously missing something here, because ember invokes my spring controller with JSON entered in the page
perfectly, data are saved, why Ember.RSVP.Promise crashes eventually at “parseerror” doesn’t make much sense.
What role plays Ember.RSVP.Promise during save exactly?
I didn’t noticed that Ember in cooperation with JQuery sets datatype attribute of Ajax call to dataType: ‘json’,
which means that jquery expects that server will return a JSON presentation. Problem was that my Spring controller was returning a void, so after I changed my update method to return the input JSON from @RequestBody for example, everything started to work like a charm…
Thanks again for help, as far as my testing of REST calls with ember concerns, I’m going to just test Ember serializer to include ID of the Ember object to be sending to REST controller…By default ID of the object is striped out.
I’m going to just test Ember serializer to include ID of the Ember
object to be sending to REST controller…By default ID of the object is
striped out.
From the DS.Serializer documentation:
NOTE: You may not want to include the ID when updating an
existing record, because your server will likely disallow
changing an ID after it is created, and the PUT request
itself will include the record’s identification.
Ahhh, ok… Yeah, so far I was taking ID of the updating record from the Spring’s @PathVariable, well ok, I can live with that…