Using RequireJS and Backbone on different pages in the CodeIgniter web application

I am building a web application with CodeIgniter at the back and Backbone / RequireJS at the front. Thus, this is not a one-page web application, but rather uses CI for many controller actions and Backbone for different views at different times. I have a RequireJS / Backbone installation, for example:

<script type="text/javascript" data-main="main" src="require.js"></script>

main.js is as follows:

require.config({
  paths: {
           jquery: "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min",
           underscore: "libs/underscore",
           backbone: "libs/backbone",
           //Lots of other stuff
          },
  shim: {
           backbone: {
              deps: ["underscore", "jquery"],
          }
          // lots of other stuff
  }
  });

require(['views/app'], function(AppView){
    var app_view = new AppView;
});

Main.js is loaded into my main template, i.e. on each page, and, in turn, loads a bunch of views that are common to the site / application headings, navigators, etc.

But on different pages, I also want to load different views in addition to the general ones. So, for example, on a page called "publish" - almost an application in itself - I:

<script type="text/javascript" src="publish.js"></script>

main.js :

//publish.js
 require(['views/publishview], function(PublishView){
 var publishView = new PublishView;
 });

, , ( ), 404 .

, , ? , , SPA, CodeIgniter.

, .

+5
1

, , :

(php)

<script type="text/javascript">
var myApp = {};
myApp.section = '<?= $section ?>'; //for exambple 'someSection'
</script>
<script type="text/javascript" data-main="main" src="require.js"></script>

- :

(main.js)

var requires = {
someSection: {files: ['views/app'], handler: function(AppView){
    var app_view = new AppView;
}}
};

var myRequire = requires[myApp.section] || null;
if(myRequire) require(myRequire.files, myRequire.handler);

... window.location.pathname myApp.section

+1

All Articles