Skip to content

Instantly share code, notes, and snippets.

@stacylondon
Created May 11, 2015 23:04
Show Gist options
  • Save stacylondon/a84333510a02e8aa1b93 to your computer and use it in GitHub Desktop.
Save stacylondon/a84333510a02e8aa1b93 to your computer and use it in GitHub Desktop.
Refactor 3 - Backbone Views
// -------------------------------------
// my-app.js
// -------------------------------------
var myApp = myApp || {};
(function($, myApp) {
'use strict';
$.extend(myApp, {
init: function() {
this.initializeViews();
},
views: {},
initializeViews: function() {
for (var view in this.views) {
if (typeof this.views[view] === 'function') {
this.views[view.substring(0, 1).toLowerCase() +
view.substring(1, view.length)] = new this.views[view]();
}
}
},
addView: function(key, view) {
if (this.views.hasOwnProperty(key)) {
throw (new Error('A view with that key already exists.'));
}
this.views[key] = view;
},
getView: function(key) {
if (!this.views.hasOwnProperty(key)) {
throw new Error('View does not exist in views collection.');
}
return this.views[key];
}
});
})(window.jQuery, window.myApp);
$(document).ready(function() {
myApp.init();
});
// -------------------------------------
// page-one-view.js
// -------------------------------------
;
(function($, _, Backbone, myApp) {
'use strict';
var PageOneView = Backbone.View.extend({
el: '#pageOneMainContainer',
events: {
'click .something': 'doSomething'
},
initialize: function() {
this.setupValidation();
},
setupValidation: function() {
},
doSomething: function() {
}
});
myApp.addView('PageOneView', PageOneView);
}(window.jQuery, window._, window.Backbone, window.myApp));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment