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

1

u/enkideridu Sep 02 '13

If you use this.model.bind (for those newer to Backbone, Model.bind was renamed to Model.on in Backbone 0.9.0 but was kept as an alias), make sure you remember to call this.model.off (or this.model.unbind) when you're removing the view, otherwise the model will keep a reference to it and it won't get garbage-collected.

It's unfortunate that listenTo doesn't have an optional context parameter. I suppose you could do

this.listenTo(this.model, 'change:prop', _.bind(this.render, this));  

But that seems slightly less elegant than just sticking _.bindAll() into the view's initialize

1

u/user84738291 Sep 02 '13

I am using model.bind, as I started writing this before 0.9. All my views have a close method, ran into the zombie problem a good while ago.

0

u/[deleted] Sep 03 '13

[deleted]

1

u/enkideridu Sep 14 '13 edited Sep 15 '13

I actually quite like "on" and thought it was exemplary of concise and intuitive method naming.

"On my cue, hit him on the head" maps quite nicely to on(cue, smash.bind(him.head))

"Off" on the other hand did take some getting used to