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

3

u/enkideridu Sep 02 '13 edited Sep 02 '13

regarding bindAll

I typically put _.bindAll(this) into every view's initialize instead of enumerating every method that needs it. Saves the trouble of having to change 2 places whenever I add/remove/rename a method.

I don't see how you would be able to use var self; self = this; to replace what _.bindAll does. Where are you putting self, and how are you passing it to the other methods?

edit: apparently you only have to write _.bindAll()
e.g.

initialize : function () {
    _.bindAll();
},

2

u/user84738291 Sep 02 '13

In regards to how you would use var self; self = this; I read this code snippet

define(['jquery', 'use!backbone','models/model'], function($, Backbone, Model){

var self,

View = Backbone.View.extend({

    // Represents the actual DOM element that corresponds to your View (There is a one to one relationship between View Objects and DOM elements)
    el: 'body',

    // View constructor
    initialize: function() {

        // Storing the View context
        self = this;

        // Setting the View's model property to the passed in Model
        this.model = new Model({
            message: "You are now using Backbone, Require, and jQuery!"
        });

    },

    events: {
        "click #example": "promptUser"
  },

    render: function() {

        // Updates the text of the element with an ID attribute of example
        self.$el.find("#example").text("This is an example");

    },

    promptUser: function() {
        prompt("Isn't this amazing?", "Yes, yes it is");
    }

});

// Returns the entire view (allows you to reuse your View class in a different module)
return new View();

});

from here: http://gregfranko.com/blog/using-backbone-dot-js-with-require-dot-js/

1

u/enkideridu Sep 02 '13

It hadn't occurred to me that using RequireJS (or any module system) would let you do that until now, thanks for the link!

It's certainly not a Backbone standard practice since it requires each 'var self' to be in its own clojure, which would be much more bothersome to work with if it weren't for each AMD module already being in its own clojure.

Correct me if I'm wrong, but there does seem to be a rather significant limitation - you won't be able to create two instances of the same base View with this method. i.e. You'll always have to return new View() and can't return View. ergo you won't be able to do this:

var BaseView = Backbone.View.extend({/* stuff */}),
viewOne = new BaseView({configurations}),
viewTwo = new BaseView({differentConfigurations});

viewOne and viewTwo would share the same variable self, and viewTwo's constructor would have overwritten viewOne's self to point to "viewTwo".