0
votes

I'm giving it a go at making an Ember Electron app and wanting to save some model data to a json or json-api formatted file instead of pushing it to a remote API, what would be the best way to do this?

I see that it is possible to call .toJSON() on model objects to get a basic JSON representation of the model, from which I can save it to disk using electron-settings, however I'm at a loss on how to load the data in.

From what I can see, using the "push" function in Ember data allows you to import data, however it seems it expects JSON-API format instead:

https://guides.emberjs.com/v2.9.0/models/pushing-records-into-the-store/

1

1 Answers

0
votes

I've found it is easiest to store the data in JSON API spec format. To get this out of the system simply call var data = model.serialize({includeId: true}) this will give you the JSON API Specced version of your data.

Or to get a load of models:

var jsonModels = [];
allModels.forEach(function(model) {
    jsonModels.pushObject(model.serialize({includeId: true}).data)
}, this);
return {data: JSON.stringify(jsonModels)};

You can then call this.get('store').pushPayload(data); to load that JSON into your store.