Skip to content

Instantly share code, notes, and snippets.

@odiroot
Created November 18, 2012 11:59
Show Gist options
  • Save odiroot/4104798 to your computer and use it in GitHub Desktop.
Save odiroot/4104798 to your computer and use it in GitHub Desktop.
Backbone.js post
var MyLayout = Backbone.View.extend({
subViews: [Foo, Bar], // This can be modified in runtime.
render: function() {
_.each(this.subViews, function(V) {
var widget = new V().render();
this.$el.append(widget.$el);
}, this);
return this;
}
});
var top = new MyTopView({
el: "#top-view"
});
var middle = new MyMiddleView({
el: "#middle-view"
});
var bottom = new MyBottomView({
el: "#bottom-view"
});
// Main JS file.
require([app], function(App) {
var app = new App();
app.start(); // Initialize and run application.
});
// app.js
define([myview], function(MyView) {
var App = function(){};
App.prototype.start = function() {
this.view = new MyView({
el: "body"
}).render();
});
return App;
});
// myview.js
define(["backbone"], function(Backbone) {
return Backbone.View.extend({
foo: "bar"
});
});
var MyModel = Backbone.Model.extend({
foo: "bar"
});
var MyCollection = Backbone.Collection.extend({
model: MyModel
});
// App initialization...
// Bootstrapping file.
ProjectFoo = this.ProjectFoo || {};
ProjectFoo.Models = ProjectFoo.Models || {};
ProjectFoo.Views = ProjectFoo.Views || {};
// Views file.
ProjectFoo.Views.MyView = Backbone.View.extend({
// [..]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment