Skip to content

Instantly share code, notes, and snippets.

@sjungling
Created January 7, 2014 21:21
Show Gist options
  • Save sjungling/8307118 to your computer and use it in GitHub Desktop.
Save sjungling/8307118 to your computer and use it in GitHub Desktop.
Sample of how to organize Backbone
/**
* Backbone View Organization
*
* 1. View options (events, tagName, el, etc.)
* 2. Initializer
* a. Should call private functions to do setup for listeners, etc.
* 3. Public Methods
* 4. Private Methods
* a. Shall begin with an underscore (_) prefix.
*
*/
var ProductView = Backbone.View.extend({
// Events
events: {
'click': '_clickHandler',
'change input': '_formHandler'
},
/**
* @constructor
**/
initialize: function() {
ProductView.__super__.initialize.apply(this, arguments);
// Establish Listners
this._setupListener();
return this;
},
// Public Methods
/**
* render setup and create HTML
*/
render: function() {
var doLogic;
if (doLogic) {
this.handleStuff();
} else {
this.doOtherStuff();
}
},
// Private Methods
/**
* _setupListener - Establish listeners
*/
_setupListener: function() {
this.on('eventA', this.render, this);
this.on('eventB', this._getStuff, this);
},
/**
* _clickHandler
* @return {void}
*/
_clickHandler: function() {
this._getStuff();
},
/**
* _formHandler
* @return {void}
*/
_formHandler: function() {
this.render();
},
/**
* _getStuff
* @return {Object} model
*/
_getStuff: function() {
return this.model;
}
});
return ProductView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment