AngularJs directive and $http calls

VishwaKumar

I have created a angular multiselect directive plugin. The directive takes an array as an input. The idea is to use this plugin in all for the present and future projects if needed. The array needs to be in the following format :

[
    { id: 1, name: 'Theme1', selected: false },
    { id: 2, name: 'Theme2', selected: false },
    { id: 3, name: 'Theme3', selected: false },
    { id: 4, name: 'Theme4', selected: false },
    { id: 5, name: 'Theme5', selected: false },
    { id: 6, name: 'Theme6', selected: false },
    { id: 7, name: 'Theme7', selected: false },
    { id: 8, name: 'Theme8', selected: false },
    { id: 9, name: 'Theme9', selected: false },
    { id: 10, name: 'Theme10', selected: false }
]

The directive takes the above input and converts to this [image url] :

http://pho.to/7wl3E

Now my colleague argues that , the directive will always have to be passed the above format, which is not fisible. He then goes on to suggest the directive have to make an API $http request to get the data from server , use the data returned to create the necessary format. He also insists to handle both pre-formatted data and $http calls in directive.

I debate back that angular directives are not made for this purpose and there involves lot of things if use $http calls inside directive like API URL, method [GET,POST], payload data for post, needs to be passed to directive, weather to preload the data for dropdown or not.

So pertaining to this scenario, here's my questions

  1. Is $http calls a good practice to use inside custom directives?
  2. The suggestion given by my colleague to have both pre-formatted data and $http calls in directive. Is it good practice?
  3. If point 2 is not a good practice , any suggestions or ideas on this scenario so that i can convience my colleague?
Viktor
  1. Is $http calls a good practice to use inside custom directives?

Depends on whether the directive is a standalone feature or not. (Lets say you're creating a directive that nurture itself and doesn't really play ball with anything else).

I would argue that $http calls should only be made in services and then injected into the controller (with bindings neccesary for the data to reach the directive) - or into the directive if you really have to.

However, keeping your $http requests to services promotes reusability.

  1. The suggestion given by my colleague to have both pre-formatted data and $http calls in directive. Is it good practice?

See previous answer.

  1. If point 2 is not a good practice , any suggestions or ideas on this scenario so that i can convience my colleague?

I would advice you both to look through one of the major styleguides made available by the Angular JS community. One being http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/.

Angular codingstyles are largely oppinionated and there's a lot ways to do it where some of them are better.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related