跨不同 amd 模块共享资源 [英] Share resources across different amd modules

查看:21
本文介绍了跨不同 amd 模块共享资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个新的网络应用程序.

I'm currently developing a new web application.

这是我第一次在 AMD 模块中使用 requirejs.

This is the first time I'm using requirejs with AMD modules.

适应这种新范式并不容易——据我所知——全局命名空间中没有变量.

It's not that easy to get used to that new paradigm that there are - as I understand it - no variables in the global namespace.

在以前的 Web 应用程序中,我总是在全局命名空间中有一个变量,我可以使用它在不同模块之间共享多个资源.

In previous web applications I always had one variable in the global namespace which I could use to share several resources across different modules.

现在使用requirejs AMD 模块,我使用backbone.js 和jquery(两个amd 版本 - jquery 1.7.1 和backbone.js 0.5.3-optamd3).

Now with requirejs AMD modules, I use backbone.js and jquery (both amd versions - jquery 1.7.1 and backbone.js 0.5.3-optamd3).

在我的应用程序的某个地方,我从服务器(用户对象)获取了一个backbone.js 模块.我想从不同的 AMD 模块访问这个模块.我还想要一个应用程序范围的事件对象.

Somewhere in my application I fetch a backbone.js module from the server (user object). I would like to have access to this module from different AMD modules. I also want to have an application wide event object.

您能告诉我:AMD 在 requirejs 中跨不同模块共享资源的正确方法是什么?

Could you tell me: what is the right way in requirejs AMD to share resources across different modules?

推荐答案

我自己找到了解决方案.

I found a solution myself.

感谢 IntoTheVoid 的回答,但我希望有一个类似 AMD 的解决方案.这意味着不再污染"全局命名空间.

Thank you, IntoTheVoid, for your answer, but I was hoping for an AMD-like solution. This means, not again, "polluting" the global namespace.

我的解决方案有两个关键:

There were 2 keys to my solution:

"https://github.com/addyosmani/backbone-aura" 来自 Addy Osmani和 "https://github.com/amdjs/amdjs-api/wiki/AMD" 异步模块定义 (AMD) API 规范.

"https://github.com/addyosmani/backbone-aura" from Addy Osmani and "https://github.com/amdjs/amdjs-api/wiki/AMD" the The Asynchronous Module Definition (AMD) API specification.

规范说:如果工厂是一个函数,它应该只执行一次."

The spec says: "If the factory is a function it should only be executed once."

因此,如果在 Web 应用程序中多次将 amd 模块指定为依赖项,则该依赖项不仅未加载多次,而且未执行多次强>,这对我来说是新事物.它只执行一次,并保留工厂函数的返回值.具有相同路径的每个依赖项具有相同的对象.这改变了一切.

So, if an amd module is specified multiple times as a dependency in an web application, the dependency is not only NOT LOADED MULTIPLE TIMES, it is also NOT EXECUTED MULTIPLE TIMES, and this is the new thing to me. It is only executed once and the return value of the factory function is kept. Each dependency with the same path has the same object. And this changes everything.

因此,您只需定义以下 amd 模块:

So, you simply define the following amd module:

define([], function() {
    var app_registry = {};
    app_registry.global_event_obj = _.extend({}, Backbone.Events);
    app_registry.models = {};
    return app_registry;
});

现在,在您想要共享资源的那些模块中,您将此 app_registry 模块声明为依赖项并写入一个:

Now, in those modules where you want to share resources, you declare this app_registry module as dependency and write in one:

define(['jquery','underscore','backbone','app_registry'], 
  function ($, _, Backbone, app_registry){
    var firstView = Backbone.View.extend({
    initialize: function() {
        _.bindAll (this, 'methodOne'); 
        this.model.bind ('change', this.methodOne);   
        this.model.fetch();
    },  
    methodOne: function() {
        app_registry.models.abc = this.model;
    }
    ...

在另一个:

define(['jquery','underscore','backbone','app_registry'], 
  function ($, _, Backbone, app_registry){
    var secondView = Backbone.View.extend({
    initialize: function() {
       _.bindAll (this, 'methodTwo'); 
        app_registry.global_event_obj.bind ('special', this.methodTwo);
    },  
    methodTwo: function() {
        app_registry. ...
    }
    ...

这篇关于跨不同 amd 模块共享资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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