Hello. I thought about learning Angular.js. Passed a couple of courses, for some time played with the application "phoneCat" from the documentation. I conceived a simple application that will collect news from various sources (optional) and display it on the page.

Now I have come to the following:

  • Created layout;
  • Created the "menu" component, which displays a menu with news filtering settings;
  • Created a component that displays the news on a preview.

It looks like this:

Those. A menu is displayed in the layout, a preview is displayed in the menu.

The problem is the following - I do not know how to organize the loading of news from various sources (say, from two dozen news sites).

I understand that for each news source you need to create your own service that will parse the site / pull the API, etc. It's not a problem. But then how to work with these dozens of services, so much so that it is convenient - I can not think of something.

Questions:

  • How to work with a large number of services?
  • Where is "more correct" to use these services? In the controller component news-preview? In the menu controller? Or is it better to create another service to work with them?
  • How to block / activate services? For example, if I want to view news from only one source.

Thank.

    1 answer 1

    I will answer closer to the classical architecture.

    How to work with a large number of services?

    Services you have one common characteristic - they get a list of news. Accordingly, for a specific implementation of services, you can make a base class adapter . All specific services will be descendant adapters. To download the news of descendants need a little - I would have managed one adapter for rss - which give 80% of news sites. Accordingly, the adapter is not a singleton, it should be possible to create several instances of the same adapter with different configurations (for example, with different rss receiving addresses).

    The design of adapters and operating with them - for good, a singleton-facade should manage, for example, the getNews method (query, filter) will be used. For the use of adapters is a brute - their architectural advantage of unification is lost.

    Where is "more correct" to use these services?

    I think in the controller of the news-preview component - he is the direct consumer of the service. The menu itself is only a filter.

    How to block / activate services?

    Filter by services (the one that the menu) - is fed to the entrance in the facade's getNews method, depending on the filter, the facade constructs certain adapters - and uses them in a loop.

    PS The term class is not understood as class ES6 in JavaScript - but any way of classifying and inheriting JS objects.

    • Is this applicable to the first angulyar? - Vladimir Gamalyan
    • This applies simply to JS, and not only to any language. We need aggregation of many services and unified work with them: facade + adapters. If you need miscellaneous replaceable services - use DI docs.angularjs.org/guide/di - but this is probably not for your task. - Goncharov Alexander
    • In an angular, as a rule, services are simply listed in the arguments of the controller, and the angular creates them when they are first used. Do you propose to overload this circuit? - Vladimir Gamalyan
    • @Vladimir Gamalian Aha. Normal DI. In the context of angulyarki - the facade for getting a list of news - you can make it an angular service, but what adapters will be and whether they will be connected with an angular is a question of implementation. - Goncharov Alexander