Backbone: Easiest way to maintain reference to 'this' for a Model inside callbacks

Posted by Garrett on Stack Overflow See other posts from Stack Overflow or by Garrett
Published on 2012-06-08T03:18:19Z Indexed on 2012/06/08 4:40 UTC
Read the original article Hit count: 212

var JavascriptHelper = Backbone.Model.extend("JavascriptHelper",
        {}, // never initialized as an instance
        {
            myFn: function() {
                $('.selector').live('click', function() {
                    this.anotherFn(); // FAIL!
                });
            },

            anotherFn: function() {
                alert('This is never called from myFn()');
            }
        }
    );

The usual _.bindAll(this, ...) approach won't work here because I am never initializing this model as an instance. Any ideas? Thanks.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about backbone.js