user3056656
user3056656

Reputation: 53

Good architecture MVC, API-calls

Me and some friends are currently doing a project where we are designing a webpage, where we do a number of api-calls using ajax.

We are using the Angular-framework, and we are wondering what is the correct architecture on where to put the API-calls. Right now we have them in our controllers, and saving the results as $scope-objects.

We are however wondering if it would actually be better praxis to have the API-calls in the model. We have been googling a lot, and can't seem to find an answer.

Upvotes: 0

Views: 67

Answers (1)

AlexMA
AlexMA

Reputation: 10192

Encapsulating API calls in services is a good idea, but don't try to hide the fact that you are making web requests in your code. Have the services/model return descriptive promises and have your controller use the promises and handle errors gracefully. If using REST, you might want to use Angular's built in $resource factory. If the code is easy to unit test, it will be a sign that you're doing a good job. Being able to easily mock the services will make your controllers a lot easier to test.

Upvotes: 3

Related Questions