Created
July 1, 2015 16:26
-
-
Save arkitrave/6045bb865a52b9236eaf to your computer and use it in GitHub Desktop.
Private methods in Backbone views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define('my_view', | |
[jquery, underscore, backbone], | |
function ($, _, Backbone) { | |
// These will all have "this" bound correctly | |
var | |
MyView, | |
helpers; | |
helpers = { | |
shouldDoSomething: function () { | |
return this.model.canEven(); | |
} | |
}; | |
MyView = Backbone.View.extend({ | |
initialize: function () { | |
var self = this; | |
_.each(helpers, function (helper, key) { | |
helpers[key] = _.bind(helpers[key], self); | |
}); | |
// carry on | |
return this; | |
}, | |
somethingRequested: function () { | |
if (helpers.shouldDoSomething()) { | |
// totes do something | |
} | |
return this; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment