在不同的模块,AMD共享资源 [英] Share resources across different amd modules

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

问题描述

我目前正在开发一个新的Web应用程序。结果

I'm currently developing a new web application.

这是我第一次使用requirejs与AMD模块。结果

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.

在previous 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和0.5.3 Backbone.js的-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.

你能告诉我:什么是requirejs正道AMD在不同的模块共享资源搜索

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

推荐答案

我找到了解决办法喽。

感谢您,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.

有2把钥匙给我的解决方案:

There were 2 keys to my solution:

https://github.com/addyosmani/backbone-aura 从阿迪·奥斯马尼和 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."

因此​​,如果AMD模块多次指定为一个Web应用程序的依赖,依赖不仅是不会加载多次,这也是不会多次执行,这是新的东西给我。它只能执行一次,工厂函数的返回值被保留。具有相同路径每个依赖性具有相同的对象。而这一切都改变了。

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天全站免登陆