Skip to content

Instantly share code, notes, and snippets.

@arkitrave
Created July 1, 2015 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkitrave/6045bb865a52b9236eaf to your computer and use it in GitHub Desktop.
Save arkitrave/6045bb865a52b9236eaf to your computer and use it in GitHub Desktop.
Private methods in Backbone views
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