backbone.js - How should I bootstrap my web app? -


i looking best way bootstrap web app using backbone.marionette, backbone.router , requirejs.
following implementation works know if right way make things.

here's of code (*).

my questions are:
1) right following data flow (index.html -> conf.js -> router.js -> app.js) ?
2) backbone.view each region (header, sidebar .....) should instantiate in router.js or app.js or booths according context?


// index.html <!doctype html> <html lang="en">     <head>         <!-- load script "/js/conf.js" our entry point -->         <script data-main="js/conf" src="js/vendor/require.js"></script>      </head>      <body>      </body> 

// js/config.js require.config({     // code }); require(['app']);  // instead of  require(['conf']);  

// router.js define([     'app',     // others modules ], function(app, $, _, backbone, marionette){     "use strict";     var approuter = backbone.marionette.approuter.extend({         routes: {             test: test,             "*defaults": "home"         }      var initialize = function ()     {         var app_router = new approuter;     };      return {         initialize: initialize     }; }); 

// js/app.js define( [     // modules ], function ($, _, backbone, router, mustache, layout, sidebarview) {     var myapp = new backbone.marionette.application();      myapp.addinitializer(function () {         $('body').html(layout);         myapp.addregions({             header: '#header',             sidebar: '#sidebar',             maincolumn: '#main-column',             rightcolumn: '#right-column'         });     });      myapp.addinitializer(function () {         var sidebarview = new sidebarview();         myapp.sidebar.show(sidebarview);     });      myapp.on("initialize:after", function () { //        router.initialize();     });      myapp.start();      return myapp;  }); 

this looks pretty overall. there few things might change, these personal preferences:

1) invert relationship between router , app files, , use initializer start router.

right have circular dependency between router , app files, , that's never thing. though requirejs can handle fine, it's bad idea in many other ways can lead code doesn't quite work way expect.

2) in router file, set initializer instantiates router.

3) don't start backbone.history router file.

it's common, , suggested, have multiple routers in project. can call backbone.history.start() once. start in app.js file, using "after:initialize" event of router

myapp.on("after:initialize", function(){ backbone.history.start(); }

4) extract initializer callbacks in functions called single initializer

while there's nothing technically wrong using multiple initializers - , need multiple initializers across multiple modules - suggest using single initializer within single module, , have 1 initializer call other functions defined in module.

5) call addregions outside of initializers

there's no guarantee initializers run in order add them. depends on how individual browser handles things.


for example, app.js file this:

 // js/app.js define( [     // modules ], function ($, _, backbone, router, mustache, layout, sidebarview) {     var myapp = new backbone.marionette.application();      myapp.addregions({         header: '#header',         sidebar: '#sidebar',         maincolumn: '#main-column',         rightcolumn: '#right-column'     });      myapp.addinitializer(function(){        showlayout();        initsidebar();     });      myapp.on("initialize:after", function(){       backbone.history.start();     });      function initsidebar() {         var sidebarview = new sidebarview();         myapp.sidebar.show(sidebarview);     }      function showlayout() {         $('body').html(layout);     }      myapp.start();      return myapp;  }); 

...

alright, looks lot more changes thought. :) said, set looks fine on all. these things do, not requirements make app work.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -