Where to format collections / objects

30 views Asked by At

From front end architectural point of view, what is the most common way to store scripts that perform transformations on collections of objects/models? In what folder would you store it, and what would you name the file / function?

Currently I have models, views, controllers, repositories, presenters, components and services. Where would you expect it?
As a component (what would you name it?)? As a service? Currently I use services to make the connection between the presenter and the repository to handle data interactions with the server.

Should I call it a formatter? A transformer? If there is a common way to do, I'd like to know about it.

1

There are 1 answers

1
Eliran Malka On BEST ANSWER

[...] models, views, controllers, repositories, presenters, components and services. Where would you expect it?

services, mos def. This is a interception service for parsing data.

Should I call it a formatter? A transformer?

Well, trasformer (or data transformer) is actually quite good IMO. data interceptor also comes to mind, and data parser, obviously.

If there is a common way to do, I'd like to know about it.

Yes, there is! Override the model's / collection's parse() function to transform the data fetched from the server into your preferred data structure.
Note that you should pass {parse: true} in the options to make it work.

This, of course, does not contradict using the services you wrote from within that function. You can encapsulate the parsing logic in those scripts, and reuse it anywhere you'd like.

Bare in mind that there will probably be very little code reuse when using parse(), as each transformation will relate to a single model or collection.