最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handling global varibales when using AMD Requirejs + Backbonejs - Stack Overflow

programmeradmin2浏览0评论

As in the application I need to have some configured variables which are to be global and I am using them across modules. My config file may look like.

config.js:

var config = {
    baseServiceUrl: 'http://localhost/baseServiceUrl',
    baseUrl: 'http://localhost/baseUrl',

    mapping_764: {
            location:'Austin',
            emailAddress:'[email protected]', 
            registerLink:'/',
            fbLikeLink:'.austin',
            microSite: '/'
        }
}

I am just loading this file using script tag along with requirejs.
<script src="js/app/config.js"></script>
<script data-main="js/main" src="js/libs/require/require.js"></script>

Now the global variable(object) config can be used in all the modules and also from browser console. So I was thinking what if someone changes this config attributes, the application is definitely going to crash because this config variable is used for service calls and for many other things.

Is there any way to handle this kind of issues.?

As in the application I need to have some configured variables which are to be global and I am using them across modules. My config file may look like.

config.js:

var config = {
    baseServiceUrl: 'http://localhost/baseServiceUrl',
    baseUrl: 'http://localhost/baseUrl',

    mapping_764: {
            location:'Austin',
            emailAddress:'[email protected]', 
            registerLink:'https://www.customercare./en/austin/registration/',
            fbLikeLink:'https://www.facebook./customercare.austin',
            microSite: 'http://austin.customercare./'
        }
}

I am just loading this file using script tag along with requirejs.
<script src="js/app/config.js"></script>
<script data-main="js/main" src="js/libs/require/require.js"></script>

Now the global variable(object) config can be used in all the modules and also from browser console. So I was thinking what if someone changes this config attributes, the application is definitely going to crash because this config variable is used for service calls and for many other things.

Is there any way to handle this kind of issues.?

Share Improve this question edited May 21, 2013 at 18:15 Steve P 19.4k7 gold badges72 silver badges96 bronze badges asked May 15, 2013 at 7:29 Sai ChaithanyaSai Chaithanya 7081 gold badge7 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

A couple things may help you:

First, for the pure "site configuration" (e.g. REST URLs) info, RequireJS has a configuration API. From the documentation:

requirejs.config({
    config: {
        'bar': {
            size: 'large'
        },
        'baz': {
            color: 'blue'
        }
    }
});

//bar.js, which uses simplified CJS wrapping:
//http://requirejs/docs/whyamd.html#sugar
define(function (require, exports, module) {
    //Will be the value 'large'
    var size = module.config().size;
});

There is also this question which addresses the general case of passing global variables around if you don't want to use the config API.

Second, for bootstrapping data into Backbone models, there is extensive discussion in this question

发布评论

评论列表(0)

  1. 暂无评论