将DurandalJS中的数据传递到其他视图 [英] Pass data in DurandalJS to other view

查看:80
本文介绍了将DurandalJS中的数据传递到其他视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DurandalJS 开发一个 SPA Web应用程序,但我不了解。

I'm developing a SPA webapplication with DurandalJS, but I don't understand something.

假设我有页1 2 。在页面1 上,有一个模式可以使用 BreezeJS在数据库中创建一个实体。 code>并返回一个密钥(该密钥是在数据库中生成的。)

Let's say I have page 1 and 2. On page 1 there is a modal that creates a entity in the database with BreezeJS and gives back a key (that was generated in the database).

我想传递此获取的密钥,然后转到第二页2.我知道将其放在url中时该怎么做

I want to pass this fetched key and then go to my second page, page 2. I know how I can do this when I put it in the url

http://localhost:60312/#/page2?orderid=2&repairorder=2

但这不是 Durandal ,还是?用户可以输入自己输入的内容,后果不一!

But this is not the way for Durandal, or is it? A user can enter a number of his own with al consequences!

我该如何解决?

推荐答案

据我所知,在Durandal应用程序的视图模型之间传递参数的唯一无缝方法是使用常规基于散列的url参数。

在您的示例中,您可以定义一个带有orderId和repairOrder参数的路由器,然后您的url如下所示: http:// localhost :60312 /#/ page / 2/2

As far as I know, the only seamless way to pass parameters between view models in a Durandal app is to use the normal hash-based url parameters.
In you example, you can define a router that takes a orderId and repairOrder parameters, then your url would look something like this: http://localhost:60312/#/page/2/2

如果您的数据不太敏感,这是最佳选择。将导航参数保留为打开状态,并在第二个视图模型中谨慎处理这些值。换句话说,设计视图模型以使其能够正确处理任何值-即使用户直接修改参数值,您的视图模型也应该能够正确处理它。

This is best option, if your data isn't too sensitive. Leave the navigation parameters in the open, and handle the values with care in the second view model. In other words, design your view model so that it can properly handle any values - even if a user directly modifies a parameter value, your view model should be able to handle it correctly.

能够直接设置url参数可能是有利的,但是在使用它们之前,您一定要确保值的完整性。

It can be advantageous to be able to set url parameters directly, but you do have to be careful to ensure the integrity of the values before consuming them.

但是,如果您需要隐藏参数,则确实有一些不同的选择:

However, if you need to hide the parameters, you do have some different options:

加密:使用像tea.js这样的加密库,并且在将该值添加为导航参数值之前,先对其进行加密。然后,当然,在使用它之前,请在第二页上对其进行解密。这样可以使Durandal的路由器导航正常工作,同时防止用户提供自己的值。

Encryption: Use an encryption library like tea.js, and encrypt the value before adding it as a parameter value for navigation. Then, of course, decrypt it on the second page before using it. This allows Durandal's router navigation to work normally, while preventing users from supply their own values.

如果您确实需要为了防止用户输入自己的值,并且您可以承担一些额外的KB的开销,这是一个好方法,只要您只需要传递一些参数即可。

If you really need to prevent users from entering their own values, and you can bear the overhead of a few extra KB's, this is a good approach as long as you only need to pass a few parameters.

共享数据模型::使用在两个视图模型之间共享的单例模型。该模型可以根据需要手动 required()进行手动操作,并且实质上是一个或多个视图模型之间共享的应用程序状态的存储库。
可以正常工作,但是有点像拥有一个大的全局变量-如果使用过度,它会变得混乱。

Shared data model: Use a singleton model which is shared between the two view models. This model can be manually required() as needed, and essentially serves as a repository for the application state that is shared between one or more view models. This works fine, but it's kind of like having a big global variable - it can get messy if it's overused.

直接修改VM: (仅适用于单例视图模型)
手动 require()第二个视图模型并设置其属性,然后再导航至该模型

Modify VM directly: (Only for singleton view models) Manually require() the second view model and set its properties before navigating to it.

这篇关于将DurandalJS中的数据传递到其他视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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