backbone-style models in angularjs?

2019-02-17 00:01发布

问题:

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:

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.