简体   繁体   中英

Save an attribute in a custom way in Ember-data

Say that I have a model, which has some attributes which are stored in a database, and another one which is stored some place else, but the client receives it as a one single JSON blob.

App.Tournament = DS.Model.extend({
  name:              DS.attr("string"),
  ...
  is_registered:     DS.attr("boolean")
});

the attribut is_registered is not a part of the model in the server, and I would like to save it separately. Here's an example

tournament = App.store.find(App.Tournament, 1);
tournament.set("name", "foo bar");
tournament.set("is_registered", true);

App.store.commit();

and this would do something like

PUT /tournaments/1 name: "foo bar"
POST /tournaments/1/register

or something like that, where an attribute is saved different than the default. Is there a simple way to do this, or am I approaching the problem in a wrong way?

I would manage this complexity on the server. The problem with saving one logical item on the client in more than 1 request is that if one of the requests failed, your data might be corrupted. If you do some sort of Facade service that coordinates what data goes where, you can wrap a transaction around the operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM