简体   繁体   English

将Silverlight MVVM应用程序移植到AngularJS

[英]Porting Silverlight MVVM app to AngularJS

We're porting an existing Silverlight application into AngularJS/Typescript. 我们正在将现有的Silverlight应用程序移植到AngularJS / Typescript中。

The application has a classical MVVM structure with data models, view-models and views. 该应用程序具有经典的MVVM结构,其中包含数据模型,视图模型和视图。 The models are somewhat tightly interconnected: for example, there are IObservable<> event streams which notify container models of changes within child models. 这些模型之间紧密地联系IObservable<> :例如,有IObservable<>事件流将子模型内的更改通知容器模型。

Here's what confuses me: in MVVM, everything that binds to view is a view-model. 这就是令我困惑的地方:在MVVM中,绑定到视图的所有内容都是视图模型。 One view-model may have a list of other view-models that it contains. 一个视图模型可能包含其包含的其他视图模型的列表。 In all AngularJS tutorials for people with WPF/Silverlight background, however, only the $scope is called a view-model. 但是,在所有WPF / Silverlight背景的AngularJS教程中,只有$scope被称为视图模型。 What about the nested objects? 嵌套对象呢? Are they just domain data models, and you bind to them directly without any intermediate view-model layer? 它们只是域数据模型,您直接绑定到它们而没有任何中间视图模型层吗?

Then, there's another question: if my data object changed somehow (for example, some service updates it when receiving data from the network, or it simply does some work based on timer), how do I notify the view that it should be updated? 然后,还有另一个问题:如果我的数据对象以某种方式发生了变化(例如,某些服务从网络接收数据时会对其进行更新,或者仅根据计时器进行某些工作即可),如何通知视图应对其进行更新?

First, I would like to say that you shouldn't try to match one technology to another technology. 首先,我想说的是,您不应该尝试将一项技术与另一项技术相匹配。 Doing an app in C# is not doing an app in Javascript (or any flavor of it) so trying to put a name on how the silverlight things are called on Angular/javascript is not a good idea. 用C#编写应用程序不是使用Javascript(或其他任何形式)编写应用程序,因此尝试在Angular / javascript上如何调用Silverlight事情起个名字并不是一个好主意。

What I would suggest is you (and your team) learn how Angular applications are made, what components are used for what purpose and then port the IDEA to angular. 我建议您(和您的团队)学习如何制作Angular应用程序,将哪些组件用于什么目的,然后将IDEA移植到angular。 Coding an Angular app like a silverlight app is not a good idea. 像Silverlight应用程序一样编码Angular应用程序不是一个好主意。

Anyway, for the sake of the answer, I'll answer some of your questions since I am familiar with WPF / Silverlight. 无论如何,为了回答这个问题,由于我对WPF / Silverlight十分熟悉,所以我会回答您的一些问题。

In angular there is no real need to implement any observation pattern (there is pubsub). 在Angular中,真正不需要实现任何观察模式(存在pubsub)。

Normally you have services which do a couple of things, like a repository pattern to access your backend or hold your data to use in different pages. 通常,您的服务具有多种功能,例如用于访问后端或保存数据以在不同页面中使用的存储库模式。 Since services are singleton, if you modify it from pageA, pageB will know, so there is no need to tell, that is implicit. 由于服务是单例的,因此,如果您从pageA对其进行修改,则pageB会知道,因此无需指出,这是隐式的。

$scope works as a glue between your controller and your view. $ scope充当控制器和视图之间的粘合剂。 Since $scope holds POJOs (plain old javascript objects you can put in there anything you need. You can have there nested objects or functions. Then you can use those properties on the $scope in the view without any problem. 由于$ scope拥有POJO(可将普通的javascript对象放置在所需的任何位置。可以在其中放置嵌套的对象或函数。然后可以在视图中的$ scope上使用这些属性,而不会出现任何问题。

If a Service query a backend, it updates the service's data and every controller using that service will automagically get the updated content. 如果服务查询后端,它将更新服务的数据,并且使用该服务的每个控制器将自动获取更新的内容。 The trick here is to know is that you don't have to kill the reference and just update the value. 这里的窍门是知道您不必杀死引用,而只需更新值即可。

So, take a little time to learn Angular without having Silverlight in mind, learn what every piece does and start working. 因此,花一点时间来学习Angular,而无需考虑Silverlight,了解每一部分的功能并开始工作。 That is my advice. 那是我的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM