与迪朗达尔会话数据 [英] Session Data with Durandal

查看:122
本文介绍了与迪朗达尔会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始接触Durandal.js所以原谅我的傻quesstion ...

I am just getting started with Durandal.js so excuse me for the silly quesstion...

当用户发出它的应用程序被要求选择一个轮廓样的'第一个请求,我需要它是在网站上所有其他浏览模式访问,我第一次虽然创造了这个属性的外壳视图模型,但不如何做到这一点。

When a user makes it's first request to the app it is asked to choose a 'profile kind', and I need it to be accessible to every other view model in the web site, I first though of creating this property in the shell viewmodel, but don't how to do it.

是如何在一个会话数据存储就像一个迪朗达尔SPA模式的最佳方式?

How is the best way to store data in a Session like mode in a Durandal SPA?

谢谢!

推荐答案

创建你需要存储哪些数据AMD的模块。

Create an amd module for what data you need to store.

然后就要求该模块为有需要的任何其他模块的依赖关系。

Then just require that module as a dependency for whatever other modules that need it.

排序是这样的:

define(function () {
    return {
        someVariable: 'value1',
        someVariable2: 'value2'
    }
})

其他模块

define(['session'], function(session) {
    return {
        getValue1: function () {
            return session.someVariable;
        },
        obs1: ko.observable(session.someVariable2)
    }
})

编辑**
AMD模块在那里不污染窗口对象的全局命名空间。但是,如果你宁愿不要求你作为一个依赖会话,并刚刚经历一个全局变量访问它那么就是完美的罚款。

EDIT** AMD modules are there to not pollute the global namespace of the window object. But if you would rather not require your session as a dependency and just access it through a global variable then that is perfectly fine.

您可以在声明它的 shell.js 如果您想和这样做:

you can declare it in your shell.js if you would like and do something like:

define(function () {
    window.session = { someVariable: 'value1', someVariable2: 'value2' };
})

和其它一些模块内部可以访问会话对象,像这样:

then inside some other module you can access the session object like so:

define(function() {
    return {
        getValue1: function () {
            return session.someVariable;
        },
        obs1: ko.observable(session.someVariable2)
    }
})

此信息不会刷新页面之间持续..其仅在内存中。
如果你想找坚持我就不会考虑持续的,除非你打算使你的应用程序的离线应用程序在客户端上的任何信息会话数据。
脱机应用程序是,即使工作瓦特/出互联网的应用程序。但是,如果你的应用程序需要用户总是连接到互联网,然后我只想会话数据存储在服务器上。所以,仅仅使用Web服务来持久保存会话数据和检索。

This information will not be persisted between page refreshes.. its only in-memory. If your looking to persist the session data I would not look into persisting any information on the client unless you planned on making your app an off-line application. An offline application is an app that works even w/out internet access. But if your app requires that the user is always connected to the internet then I would just store the session data on the server. So, just use web services to persist the session data and retrieve it.

您可以通过使用一个Cookie在服务器上扎会话到客户端。

You can tie the session on the server to the client by using a cookie.

这篇关于与迪朗达尔会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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