angularjs 中的骨干样式模型? [英] backbone-style models in angularjs?

查看:21
本文介绍了angularjs 中的骨干样式模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果此问题已在其他地方得到解答,我深表歉意,但我找不到完整、明显(至少对我而言)的解决方案.

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

我对主干模型的想法有经验.在我看来,它类似于任何其他 OOP 语言中的类——使用 Backbone.Model.extend() 创建一个类",并在需要该类的新实例时调用它.如果我有一个应用程序命名空间,我可以在那里存储我的所有对象,并执行类似 App.getAllThisOrThatTypeModel() 之类的操作.

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().

这是 angular 的服务吗?拥有大量服务(每种类型的模型一个),基本上用服务"模仿类",这是否是一种最佳实践"?

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.

谢谢!

推荐答案

在 Angular 中,模型只是一个 POJO(普通的 javascript 对象).

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.

我不是 Angular 专家,但我希望这能有所帮助.

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

这篇关于angularjs 中的骨干样式模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆