Javascript AMD模式混乱 - 持久的属性值 [英] Javascript AMD pattern confusion - persisting property values

查看:86
本文介绍了Javascript AMD模式混乱 - 持久的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图使用 requireJS 来了解javascript AMD模式。我对JS的经验是有限的,毫无疑问这是妨碍我的。如果我指向JS for Dummies的方向,我不会感到惊讶。如果有人能够帮助我,我将非常感激。

So I am trying to get my head around the javascript AMD pattern using requireJS. My experience with JS is limited and no doubt this is what is hampering me. I would not be surprised if I am pointed in the direction of "JS for Dummies". If anyone can bear to help me with this I would be VERY grateful.

我已经定义了一个名为security的模块,它具有以下定义 - 减少一点。

I have defined a module called "security" which has the following definition - cut down a bit.

define(['services/storage'], function(storage) {

var security = {
    authToken: {},
    loadAuthToken: loadAuthToken,
    saveAuthToken: saveAuthToken,
};

return security;

function saveAuthToken(token) {
    if (token) {
        storage.save("DiffusrAuthToken", token);
        security.authToken = token;
    }     
};

function loadAuthToken() {       
    if (security.authToken.expires) {
        return security.authToken;
    } else {
        var token = storage.load("DiffusrAuthToken");
        security.authToken = token;
        return token;
    }
};
})

基本上我想保存模块中定义的某些值的状态,在上面的例子中,的authToken 。这样,当其他模块需要这个模块时,我不必去 localStorage 来获取它。我能让它工作的唯一方法是定义返回对象的属性,并最初将其设置为空对象。这似乎并不令人满意,因为我不想直接将它暴露给其他模块,因为它们可能会改变它。

Essentially I want to save the state of some values defined in the module, in the case above, the authToken. This is so that when other modules require this module I don't have to go to localStorage to get it. The only way I can get this to work is by defining a property of the return object, and setting this to an empty object initially. This does not seem satifactory, because I do not want to expose it directly to other modules as they could change it.

非AMD的方式是在全局范围内定义一个对象并为其附加属性。但是,我认为AMD的重点是要避免这种情况。

The non-AMD way would be to define an object in the global scope and attach properties to it. BUT, I thought the whole point of AMD was to avoid this.

问题:一般来说,我应该如何在模块中设置值可以从其他模块访问吗?

Question: How, generally, should I set values in modules that can be accessed from other modules?

推荐答案

您在模块中导出的任何内容都无法在模块外部访问但仍可用在模块闭包内。例如,您可以创建var但从不将其作为 security 导出对象的属性附加。您可以为导出对象创建特定的访问器方法,该方法只能以您希望模块用户访问此私有变量的方式访问,就像您使用 saveAuthToken 一样 loadAuthToken methods。

Anything you don't export in a module cannot be accessed outside the module but is still available within the module closure. For example, you can create a var but never attach it as a property of the security export object. You can create specific accessor methods to the export object that can access this private variable only in a way that you want users of the module to, as you have done with your saveAuthToken and loadAuthToken methods.

define(['services/storage'], function(storage) {

var authToken;  // private to security module

function saveAuthToken(token) {
    if (token) {
        storage.save("DiffusrAuthToken", token);
        authToken = token;  // store to private variable
    }     
};

function loadAuthToken() {       
    if (authToken.expires) {
        return authToken;
    } else {
        var token = storage.load("DiffusrAuthToken");
        authToken = token;
        return token;
    }
};

// export contains no reference to authToken
var security = {
    loadAuthToken: loadAuthToken,
    saveAuthToken: saveAuthToken,
};

return security;

});

这篇关于Javascript AMD模式混乱 - 持久的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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