r/javascript • • Sep 02 '13

Backbone and RequireJS module definition question

Hello, I've been developing a Backbone app with different modules, and as it's now grown larger I'm looking to organise it with the AMD pattern and RequireJS.

I've been looking at different tutorials and articles about how to go about this. I have a couple of questions though, and was hoping someone here could help me out.

  1. Is there any major difference between these two Module definitions, I personally think the second is neater when it comes to a large amount of definitions?

define(function (require) {
        
           "use strict";
        
            var $           = require('jquery'),
                Backbone    = require('backbone'),
                Module1      = require('Module1'), 
         
                Employee = Backbone.Model.extend({
         
                    urlRoot: "/employees",
         
                    initialize: function () {
                        this.reports = new EmployeeCollection();
                        this.reports.url = this.urlRoot + "/" + this.id + "/reports";
                    }
         
                });

and:

define(['jquery', 'backbone', 'Module1'], function (jQuery, Backbone, Module1) {
        
           "use strict";
        
            var Employee = Backbone.Model.extend({
         
                    urlRoot: "/employees",
         
                    initialize: function () {
                        this.reports = new EmployeeCollection();
                        this.reports.url = this.urlRoot + "/" + this.id + "/reports";
                    }
         
                });

2. In the initialisation of my views, is there any problem using

_.bindAll(this, 'render', 'confirmDelete', 'edit');

over

var self; self = this;

I've already used _.bindAll in almost all my views, so I'd prefer to not have to change that.

Any comments would be greatly appreciated.

5 Upvotes

16 comments sorted by

View all comments

Show parent comments

2

u/enkideridu Sep 02 '13

Binding every function is exactly the point.

Sure it might be slower, but only by around 0.000013 seconds per object. You'll only notice a ~100ms difference when you need to initialize 8000 views at the same time, at which point you have a totally different problem.

Take a look at this [sample View from Addy Osmani's Backbone Fundamentals] (https://github.com/addyosmani/backbone-fundamentals/blob/gh-pages/practicals/todo-jqm-app/app/modules/view/abstract/BasicView.js?source=cc)

Imagine having to keep track of which methods need to be bound and enumerated, and every time you remove or rename a method you have to scroll up to change the parameter of bindAll otherwise you'll get an error and the code won't run, but only if it was an enumerated and bound method. Now imagine doing that for every view your application has. That is too much mental overhead.

1

u/user84738291 Sep 02 '13

Thanks for the info. Think I'll be switching to _.bindAll();