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

1

u/jonathanrmumm Sep 02 '13

By default, the first definition pulls in the dependencies at run-time, the second at "compile"-time. The main practical difference here is when you run the RequireJS optimizer, in the first definition, those dependencies will not be included in the optimized module, whereas in the second one it will (unless you specify findNestedDepencies: true in your mainConfigFile).

1

u/user84738291 Sep 02 '13

Thanks for the reply, if I understand correctly in the first bit of code, jQuery Backbone and Module1 will be retrieved from the sever on page load after optimisation has run (unless I set findNestedDepencies) instead of being included in 'bundle.js'

2

u/jonathanrmumm Sep 02 '13

That's correct. I didn't understand your second question but I don't see why you wouldn't be able to continue using bindAll.

1

u/user84738291 Sep 02 '13

Okay, Thanks!

Second question was just asking, any major difference between using underscore's bind function to keep the context of the view, or to store the context in a variable named self.

So in a click handler for the view I would either go

(using _.bindAll )

this.model

or (using var self = this )

self.model

But I think you're correct, I can't see any reason why I wouldn't continue this way. I can't see it making a big difference.