mardi 7 juin 2016

EmberJs does not update record after it was saved

When I create a record via const myRecord = this.store.createRecord('myType', myObject) and then myRecord.save() the request is send via adapter to server. Data are successfully saved and server returns all data back to client. It goes back through serializer, via the normalize() hook.

Problem is that Ember does not update the myRecord object with properties returned from server, for example id or version ...

When I refresh the page, all properties are there (of course).

I have similar issue with updating records. Each record in my application has a version property which is checked by server. The version is incremenented on each save. This is to for data safety. Problem is that when I try to update the record several times, then only the first attempt succeeds. The reason is that the version is not updated after the request gets back from server. (And yes, server returns the updated version)

This is a surprising behaviour for me and it seems to me like an intended functionality as suggested here - http://ift.tt/1tdY0Yf. (but that post is from 2013 and suggested solution does not work for me).

Any ideas?

For completenes, here is the relevant code (simplified and renamed)

myModel

export default Ember.Model.extend({
    typedId: attr('string') // this serves as an ID
    version: attr('string'), 
    name: attr('string'),
    markup: attr('number'), 
});

myAdapter

RESTAdapter.extend({
    createRecord(store, type, snapshot) {
        // call to serializer
        const serializedData = this.serialize(snapshot, options);

        const url = 'http://some_internal_api_url';
        // this returns a promise
        const result =  this.ajax(url, 'POST', serializedData);

        return result;
    },
});

mySerializer

JSONSerializer.extend({ serialize(snapshot, options) {
var json = this._super(...arguments); // perform some custom operations // on the json return json; },

normalize(typeClass, hash) {
    hash.data.id = hash.data.typedId;

    hash.data.markup = hash.data.attribute1;
    hash.data.version = parseInt(hash.data.version, 10);

    return hash;

} });




Aucun commentaire:

Enregistrer un commentaire