Should the embedded data in Ember have one or more Controller arrays?

I am working on proving the concept of an application, and I have a question about data storage.

There is a client model in my application

PM.Client = Ember.Object.extend({ id: null, client: null, projects: {} }); 

and project model

 PM.Project = Ember.Object.extend({ id: null, title: null, totalHours: null, cost: function(){ return this.get('totalHours') * PM.get('rate'); }.property('totalHours') }); 

Each client can have several projects, but each project can have only one client. I currently have a dummy JSON file with the following data

 [ { "id": "1", "client": "Fastbook", "projects": [ { "id": "1", "title": "Website redesign", "totalHours": "45", "cost": "4500" }, { "id": "2", "title": "Tidy up admin section", "totalHours": "10", "cost": "1000" } ] }, { "id": "2", "client": "Epicenter", "projects": [ { "id": "1", "title": "Chaching", "totalHours": "25", "cost": "2500" } ] } ] 

What is the best way to store this data in Ember? Should I have an arrayController for clients and another for projects?

Ember data might be fine, but I don't plan on setting REST for this. Can Ember Data use localStorage?

+4
source share
4 answers

It's hard to say without details about what the application will do.

I would use ArrayController for clients. I do not see the need for it to be for projects, since this data is nested inside client objects. If you have a screen on which a project is selected, you may want to have a controller for the currently selected project.

If you could describe in more detail what you are going to do with the project data, I would better advise.

+1
source

I have nested data in the reader for reading ember I am working.

I use 2 array controllers. I have a set of feeds and each feed contains entries.

For my use, when selecting a feed should show you the records it contains, I have feedController, selectedFeedController, entryController and selectedEntryController.

This allows me pretty fine-grained control. I hope this encourages you to get started, and perhaps find out if this model is right for you.

+1
source

I assume that in the final wash a detailed presentation of the client with an attached list of projects and a list of projects with all active projects on all clients will appear.

Let the use cases dictate the object model, and not the use model dictate the use cases. If you need the script above, you are likely to jump through hoops if you only have an ArrayController client; in this case, it is more natural to have both client and project ArrayControllers.

0
source

I agree with @David and @ebryn. It completely depends on the use case. Do you always use user and project data together? Then there is no need for separate array controllers, especially since the data is already nested.

But if you want to go for a clear separation of problems, then you really should consider two separate controllers.

Also, to read data from localstorage, you will need to define your own adapter, there is no built-in method. The steps required to create your own adapter are described in detail in the gibub ember data page at the end of the readme.

0
source

Source: https://habr.com/ru/post/1415783/


All Articles