7

I apologize if this question has already been answered elsewhere, but I couldn't find a full, obvious (to me, at least) solution.

I have experience with the idea of a Model in backbone. In my mind, it's similar to a class in any other OOP language - create a "class" using Backbone.Model.extend(), and call new on it when you want a new instance of that class. If I have a app namespace, I can store all of my objects there, and do something like App.getAllThisOrThatTypeModel().

Is this a service in angular? Is it an ok "best practice" to have a LOT of services (one for each type of model), basically mimicking "class" with "service"?

I'm just trying to wrap my head around the best way to deal with models as I try to migrate from backbone to angular to experiment - any advice for someone migrating in this direction would be greatly appreciated.

Thanks!

1 Answer 1

2

In Angular models are just a POJOs (plain old javascript objects).

Services are something different, but you'll usually fetch the models with services. It is a good practice to have more services, so you can reuse them throughout your application. Also a big plus for services is, that they can be unit tested. And like everything else in software engineering, it's always better to split your application into little modules which are easier to maintain than a big chucks of code.

You don't need to just mimic your models as service. You can later add some functions to them and keep your domain logic inside those services.

I'm not an Angular specialist, but I hope this can help.

Sign up to request clarification or add additional context in comments.

2 Comments

So, for example, if I had an Account "model" (which is just a POJO) - my best bet would be to have a service, use DI to get a handle on it in a controller (kinda like a singleton?), and let that get my account object? If I had many different types of models - a service per type of model? Say I had a bunch of todos...would building a $todo service make sense? Thanks a lot for your answer, sorry for my late response.
You could that way, but you don't have to. I mean, if you look at docs.angularjs.org/tutorial/step_05 you just use $http as a service to get your POJO-s. But, as I said, if you don't want to pollute your controllers and have some domain (POJO) specific code it may be a good practice to put it into separate service. I believe that Angular isn't that kind of framework, which sets all those options for you, but you need to figure it out yourself. Still it's pretty young and maybe with books about it we will get some more knowledge about best practices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.