I'm trying to modify my JSON payload in the REST serializer so I can use it in my Ember application as I want.
This is how the real JSON from the server looks like:
{
"folders": [
{
"file_ids": [
2001,
2002,
2003
],
"child_folder_ids": [
2
],
"id": 1,
"name": "Root folder",
"parent_folder_id": null,
"parent_folder_ids": []
}
],
"related_folders": [
{
"file_ids": [],
"child_folder_ids": [
3
],
"id": 2,
"name": "Child folder",
"parent_folder_id": 1,
"parent_folder_ids": [
1
]
}
]
}
This is how the Ember folder model looks like:
export default DS.Model.extend({
name: DS.attr('string'),
files: DS.hasMany('file'),
childFolders: DS.hasMany('folder', {inverse: 'parentFolder'}),
parentFolders: DS.hasMany('folder'),
parentFolder: DS.belongsTo('folder', {inverse: 'childFolders'}),
});
The childFolders and parentFolders are inside the related_folders in the JSON I'm getting from the server. Basically this are also folders so I want to use the folder model for this. I've tried to modify the payload in the REST serializer so the related folders will be part of the folders array.
This is how my serializer looks like:
export default DS.RESTSerializer.extend({
extractArray: function(store, type, payload) {
var rootFolders = payload.folders;
var childFolders = payload.related_folders;
var folders = [];
rootFolders.forEach(function(rootFolder) {
folders.push(rootFolder);
});
childFolders.forEach(function(childFolder) {
folders.push(childFolder);
});
payload = { folders: folders };
return this._super(store, type, payload);
}
});
At this moment I have my folders and related folders both in the store as folders but somehow I can not use the folder properties in my controller or components to use them in computed properties. I would like to determine which folders are root folders and which are child folders and then use the handlebars each helper in my template to show the right folders. Does anyone have an idea what I'm doing wrong or if there is a better way to fix this?
Aucun commentaire:
Enregistrer un commentaire