简体   繁体   English

大型骨干网/ RequireJS应用程序上的全球资源共享

[英]Global Resource Sharing on large Backbone / RequireJS App

I am building my first large Backbone + Requiere APP and i came across a scenarios where it would just make sense to access some kind of an "app wide" scope. 我正在构建我的第一个大型Backbone + Requiere APP,我遇到了访问某种“应用范围广泛”范围的方案的情况。 I was thinking of making a new namespace in the global scope, something like: 我正在考虑在全局范围内创建一个新的名称空间,例如:

window.APP = {
  someProp : someValue,
  ....
}

Are there any other options / good practices ? 还有其他选择/良好做法吗?

Thanks 谢谢

I always try to avoid global scoping when working with require.js, better is to avoid it. 在使用require.js时,我总是尽量避免全局作用域,最好是避免这种情况。

A solution is to create a config.js file: 一种解决方案是创建config.js文件:

#config.js

define([], function() {
    return {
        someProp : someValue
    }
})

Then when you need to access those values in another module, you can do: 然后,当您需要在另一个模块中访问这些值时,可以执行以下操作:

#foo-module.js

require(['config', 'backbone', 'underscore'], function(config, Backbone, _) {
    console.log(config.someProp)
})

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

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