Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a solution myself.</p> <p>Thank you, IntoTheVoid, for your answer, but I was hoping for an AMD-like solution. This means, not again, "polluting" the global namespace.</p> <p>There were 2 keys to my solution:</p> <p>"<a href="https://github.com/addyosmani/backbone-aura" rel="nofollow">https://github.com/addyosmani/backbone-aura</a>" from Addy Osmani and "<a href="https://github.com/amdjs/amdjs-api/wiki/AMD" rel="nofollow">https://github.com/amdjs/amdjs-api/wiki/AMD</a>" the The Asynchronous Module Definition (AMD) API specification.</p> <p>The spec says: "If the factory is a function it should only be executed once."</p> <p>So, if an amd module is specified multiple times as a dependency in an web application, the dependency is not only <strong>NOT LOADED MULTIPLE TIMES</strong>, it is also <strong>NOT EXECUTED MULTIPLE TIMES</strong>, 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.</p> <p>So, you simply define the following amd module:</p> <pre><code>define([], function() { var app_registry = {}; app_registry.global_event_obj = _.extend({}, Backbone.Events); app_registry.models = {}; return app_registry; }); </code></pre> <p>Now, in those modules where you want to share resources, you declare this app_registry module as dependency and write in one:</p> <pre><code>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; } ... </code></pre> <p>and in the other:</p> <pre><code>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. ... } ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload