Note that there are some explanatory texts on larger screens.

plurals
  1. POSet my Backbone/Requirejs Application to independant plugin reusable
    primarykey
    data
    text
    <p>I developped an application with <code>Backbone</code> In order to avoid multiple js file declaration in index.html I used <code>RequireJS</code> to load js files. Everything works fine. My application looks like this.</p> <p>index.html</p> <pre><code>&lt;body&gt; &lt;div id="#plugin-container"&gt;&lt;/div&gt; &lt;script data-main="my-plugin" src="assets/js/require.js"&gt;&lt;/script&gt; &lt;/body&gt; </code></pre> <p>my-plugin.js</p> <pre><code>define([ 'src/app' ], function(app){ app.init(); }); </code></pre> <p>app.js</p> <pre><code>define([ 'jquery', 'underscore', 'backbone', 'src/models/main' ], function($, _, Backbone, Main){ var init = function () {....} }); </code></pre> <p>Everyting works fine, but now I would like to make it a plugin, that you could use in all html project (like a <code>jQuery</code> plugin). And I don't how to proceed... The best final use could looks like :</p> <p>newproject.html</p> <pre><code>&lt;div id="#plugin-container"&gt;&lt;/div&gt; &lt;!-- Just load plugin. Do nothing. --&gt; &lt;script data-main="my-plugin" src="assets/js/require.js"&gt;&lt;/script&gt; &lt;script&gt; // Just an example that I would do but is sure not working.... myPlugin.init({ param1: true, param2: '/url', onSubmitSomething : myMethod }) &lt;/script&gt; &lt;/body&gt; </code></pre> <p>my-plugin.js</p> <pre><code>define([ 'src/app' ], function(app){ var myPlugin = app; return myPlugin; }); </code></pre> <p>My question : How to get instance of my application after requirejs load (<code>myPlugin</code>) and launch his methods in html file (like in code above) ? Maybe it's not a good way... Thx.</p> <p><strong>Update</strong></p> <p>I have try something. Add in <code>my-plugin.js</code> my main app model to the window object like this :</p> <pre><code>// Loading main application file define([ 'src/app' ], function(app){ global = window; global.myPlugin = app; }); </code></pre> <p>And when I try to launch <code>myPlugin.init()</code> in my html file <code>myPlugin</code> is not again accessible :</p> <pre><code>&lt;script data-main="mvo-thumbnail-plugin" src="assets/js/require.js"&gt;&lt;/script&gt; &lt;script&gt; // Not working... myPlugin.init({ "wrapper" : $("#thumbnailsManager"), "url_images" : "assets/", }) &lt;/script&gt; </code></pre> <p>But when I use one setTimeout to wait requireJS initiliazation it works fine : </p> <pre><code>&lt;script data-main="mvo-thumbnail-plugin" src="assets/js/require.js"&gt;&lt;/script&gt; &lt;script&gt; // If I wait 500ms requireJS has done and my window.myPlugin // object is available setTimeout(function(){ myPlugin.init({ "wrapper" : $("#thumbnailsManager"), "url_images" : "assets/" }) },500) &lt;/script&gt; </code></pre> <p>It's not a real solution for me. But I have maybe a new way of reflexion...</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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