0

I use a Backbone View to start my application using the code below. All it really does is encapsulate other objects I want to instantiate or load.

Is this O.K. or should I re-factor?

    // App
    //
    //
    //
    var BVApp = Backbone.View.extend({
        Name: 'BVApp',
        el: window,
        initialize: function () {
            this.initIndependentConstructors();
            this.initBBViews();
            this.initComposite();
            this.initBVArc();
        },
        initIndependentConstructors: function (){
            new ImageLoader();
            new SiteMorpher();
            new AccountUpdater();
            new SignOut();
        },
        initBBViews: function () {
            new BVAccountExist();
            new BVAccountCreator();
            new BVAccountButton();
            new BVAccountCode();
            new BVFaveCreator();
        },
        initComposite: function () {
            var token = ClientStorage.getToken();
            this.CV = new BVComposite();
            this.CV.renderCommon();
            if (token) this.CV.render(token);
        },
        initBVArc: function () {
            this.BVArcInstance = new BVArc({el: window, collection: new BCArc([], {data: {model: "ArcReader"}})});
        }
    });
    var App = new BVApp();
}());
  • 1
    [Code Review](https://codereview.stackexchange.com/) would be a better to ask this (check the rules there first). That being said, the purpose of Backbone is to make MVC app with small reusable components. Your view here is not reusable at all, but it's not a good example since the _"App"_ top level view is rarely reusable. We would need to see other examples. – Emile Bergeron Apr 21 '17 at 18:38
  • Well ... since you are here, do you have any input to the structure of my code ... anything you notice that is good / bad ... I would prefer that the module self - instantiate some how so I don't have a dangling new call ... –  Apr 21 '17 at 18:40
  • One thing that I can say on this example though is that `initialize` serve to initialize this view. Rendering should be done in the `render` function to keep the responsibilities separated. – Emile Bergeron Apr 21 '17 at 18:41
  • Another good practice is to keep a reference of any child view instanciated to be able to properly destroy it later by calling `remove` on it. This avoids memory leaks. – Emile Bergeron Apr 21 '17 at 18:42
  • `el: window` don't use the `window` object as the element for the views. Instead, use like a specific `div` for the app, and let the app give its own sub-element to the subviews, or append the subviews dynamically created element inside the app element. – Emile Bergeron Apr 21 '17 at 18:44
  • In what case would I want to destroy a view. My app is pretty light weight ... I don't currently have needs for this ... thanks fo the input though. –  Apr 21 '17 at 18:44
  • I mean, I am just using the View module to encapsulate code ... It's not really a view ... –  Apr 21 '17 at 18:45
  • the el tag is just for events ... so I don't actually need it at all ... –  Apr 21 '17 at 18:46
  • You should encapsulate your code inside your own structures if they're not views. Use Backbone views for what they are, to manage a DOM element and its events, to easily bind data to elements in the page. – Emile Bergeron Apr 21 '17 at 18:46
  • The [`el` property](http://backbonejs.org/#View-el) is used by Backbone by default and it creates a jQuery object of it in the background. – Emile Bergeron Apr 21 '17 at 18:47
  • Well ... yea ... but how do other people do it ... I do have my own structure but ... then people cry that It's not "industry standard" ... I can't win. –  Apr 21 '17 at 18:48
  • I understand your feeling. You should familiarize with MVC if you're not already confortable with the concept. Then from there, develop additional "components" like services (shared object instances), factories, etc. Also, you may think that your views are not really views, but in fact, you should think of views as small components. A datepicker for example is a component made from a view and a model. A list is another component made from a view that is going to render a sub-view for each element in its collection. – Emile Bergeron Apr 21 '17 at 18:54
  • Your app is a component made from a layout component, which is navigation component and a content section component, and so on, down to our datepicker example. – Emile Bergeron Apr 21 '17 at 18:56
  • sorry it's on your profile –  Apr 21 '17 at 18:58
  • I have a list on my profile of a lot of extensive answers I wrote on Backbone, you should take a look. – Emile Bergeron Apr 21 '17 at 19:00
  • Can you name one component I should probably be using and I'll look into it. –  Apr 21 '17 at 19:12
  • If you're starting a new project, you should definitely take a look at [Marionette](https://marionettejs.com/) which transforms the tools of Backbone into a full-fledged framework. – Emile Bergeron Apr 21 '17 at 19:25
  • A simple [layout and module pattern](http://stackoverflow.com/a/41082149/1218980) example, [making a good base class](http://stackoverflow.com/a/40982556/1218980), [avoiding memory leaks with Backbone](http://stackoverflow.com/a/41595430/1218980) just to link a few. – Emile Bergeron Apr 21 '17 at 19:27
  • 1
    Thanks so much ... if you wanna collab some time let me know ... here is my web app - http://www.arcmarks.com –  Apr 21 '17 at 20:16

0 Answers0