I have files and folders. Folders contain files.
The JSON that I get from the server looks like this:
folder: {id: 5, name: "my stuff" }
file: {id: 3, name: "my file", folderId: 5}
file: {id: 5, name: "my file 2", folderId: 5}
Whats the best way to build a Folder - File structure? Nest the files in the folder like:
{id: 5, name: "my stuff" files: [
{id: 3, name: "my file", folderId: 5},
{id: 5, name: "my file 2", folderId: 5}
]},
{id: 6, name: "other things" files: [
{id: 1, name: "my file", folderId: 6},
{id: 2, name: "my file 2", folderId: 6}
]}
Or use a filter that only returns the files that belong to a folder id? Like:
<li ng-repeat="folder in folders">
<ul>
<li ng-repeat="file in files | fileInFolder:folder.id"></li>
</ul>
</li>
Files can be moved to different folders!
The "best way" is always whatever way it's easiest to maintain. For me, personally, I like option 2 (the filters) because it fits the data you're getting. However, option 1 (the json structure) is a much better representation of the data, IMO. If you're stuck with that data structure, then I guess option 2 would be my choice.
... but I don't know anything about your application structure. Is this persisted to the database? When is it persisted? Instantaneously? Or only after a save has been called? Lots of factors will play into what structure will work best for you and be more maintainable for your particular solution.