Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbone-style models in angularjs?

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!

like image 891
Hoopes Avatar asked Oct 06 '12 18:10

Hoopes


1 Answers

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.

like image 144
zigomir Avatar answered Oct 05 '22 07:10

zigomir