Skip to content

Commit

Permalink
refactored github-viewer to the latest layoutmanager and code practices
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Oct 7, 2012
1 parent 3c62113 commit 69c2b34
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 534 deletions.
55 changes: 18 additions & 37 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,24 @@ function($, _, Backbone) {

// Configure LayoutManager with Backbone Boilerplate defaults.
Backbone.LayoutManager.configure({
// Allow LayoutManager to augment Backbone.View.prototype.
manage: true,

paths: {
layout: "app/templates/layouts/",
template: "app/templates/"
},
prefix: "app/templates/",

fetch: function(path) {
// Concatenate the file extension.
path = path + ".html";

// If cached, use the compiled template.
if (JST[path]) {
return JST[path];
}

// Put fetch into `async-mode`.
var done = this.async();

if (JST[path]) {
return done(JST[path]);
}

// Seek out the template asynchronously.
$.get(app.root + path, function(contents) {
done(JST[path] = _.template(contents));
});
Expand All @@ -64,36 +66,15 @@ function($, _, Backbone) {
return _.extend({ Views: {} }, additionalProps);
},

// Helper for specific layouts.
useLayout: function(name) {
// If already using this Layout, then don't re-inject into the DOM.
if (this.layout && this.layout.options.template === name) {
return this.layout;
}

// If a layout already exists, remove it from the DOM.
if (this.layout) {
this.layout.remove();
}

// Create a new Layout.
var layout = new Backbone.Layout({
template: name,
className: "layout " + name,
id: "layout"
});

// Insert into the DOM.
$("#main").empty().append(layout.el);

// Render the layout.
layout.render();

// Cache the reference on the Router.
this.layout = layout;
// Helper for using layouts.
useLayout: function(name, options) {
// Create a new Layout with options.
var layout = new Backbone.Layout(_.extend({
el: "#main"
}, options));

// Return the reference, for later usage.
return layout;
// Cache the refererence.
return this.layout = layout;
}
}, Backbone.Events);

Expand Down
1 change: 0 additions & 1 deletion app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require([
],

function(app, Router) {
var lol;

// Define your master router on the application namespace and trigger all
// navigation from this instance.
Expand Down
13 changes: 7 additions & 6 deletions app/modules/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,24 @@ function(app, Backbone) {

beforeRender: function() {
this.$el.children().remove();
this.collection.each(function(commit) {

this.options.commits.each(function(commit) {
this.insertView(new Commit.Views.Item({
model: commit,
repo: this.collection.repo,
user: this.collection.user
repo: this.options.commits.repo,
user: this.options.commits.user
}));
}, this);
},

cleanup: function() {
this.collection.off(null, null, this);
this.options.commits.off(null, null, this);
},

initialize: function() {
this.collection.on("reset", this.render, this);
this.options.commits.on("reset", this.render, this);

this.collection.on("fetch", function() {
this.options.commits.on("fetch", function() {
this.$el.html("<img src='/assets/img/spinner.gif'>");
}, this);
}
Expand Down
12 changes: 6 additions & 6 deletions app/modules/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ function(app, Backbone, Commit) {

serialize: function() {
return {
count: this.collection.length
count: this.options.repos.length
};
},

beforeRender: function() {
var active = app.router.commits.repo;
var active = this.options.commits.repo;

this.collection.each(function(repo) {
this.options.repos.each(function(repo) {
if (repo.get("name") === active) {
app.active = repo;
}
Expand All @@ -102,13 +102,13 @@ function(app, Backbone, Commit) {
},

cleanup: function() {
this.collection.off(null, null, this);
this.options.repos.off(null, null, this);
},

initialize: function() {
this.collection.on("reset", this.render, this);
this.options.repos.on("reset", this.render, this);

this.collection.on("fetch", function() {
this.options.repos.on("fetch", function() {
this.$("ul").parent().html("<img src='/assets/img/spinner.gif'>");
}, this);
}
Expand Down
16 changes: 10 additions & 6 deletions app/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function(app, Backbone, Repo) {

tagName: "li",

serialize: function() {
data: function() {
return { model: this.model };
},

Expand Down Expand Up @@ -73,12 +73,16 @@ function(app, Backbone, Repo) {
User.Views.List = Backbone.View.extend({
template: "user/list",

serialize: function() {
return { collection: this.collection };
data: function() {
return { collection: this.options.users };
},

cleanup: function() {
this.options.users.off(null, null, this);
},

beforeRender: function() {
this.collection.each(function(user) {
this.options.users.each(function(user) {
this.insertView("ul", new User.Views.Item({
model: user
}));
Expand All @@ -91,9 +95,9 @@ function(app, Backbone, Repo) {
},

initialize: function() {
this.collection.on("reset", this.render, this);
this.options.users.on("reset", this.render, this);

this.collection.on("fetch", function() {
this.options.users.on("fetch", function() {
this.$("ul").parent().html("<img src='/assets/img/spinner-gray.gif'>");
}, this);
},
Expand Down
58 changes: 22 additions & 36 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@ function(app, Repo, User, Commit) {
},

index: function() {
// Reset the state and render.
this.reset();

// Use the main layout.
app.useLayout("main");
},

org: function(name) {
// Reset to initial state.
// Reset the state and render.
this.reset();

// Use the main layout.
app.useLayout("main");

// Set the organization.
this.users.org = name;

Expand All @@ -41,12 +36,9 @@ function(app, Repo, User, Commit) {
},

user: function(org, name) {
// Reset to initial state.
// Reset the state and render.
this.reset();

// Use the main layout.
app.useLayout("main").render();

// Set the organization.
this.users.org = org;
// Set the user name.
Expand All @@ -58,12 +50,9 @@ function(app, Repo, User, Commit) {
},

repo: function(org, user, name) {
// Reset to initial state.
// Reset the state and render.
this.reset();

// Use the main layout.
app.useLayout("main").render();

// Set the organization.
this.users.org = org;
// Set the user name.
Expand Down Expand Up @@ -103,29 +92,26 @@ function(app, Repo, User, Commit) {
},

initialize: function() {
// Set up the users.
this.users = new User.Collection();
// Set the repos.
this.repos = new Repo.Collection();
// Set up the commits.
this.commits = new Commit.Collection();
var collections = {
// Set up the users.
users: new User.Collection(),

// Set the repos.
repos: new Repo.Collection(),

// Set up the commits.
commits: new Commit.Collection()
};

// Ensure the router has references to the collections.
_.extend(this, collections);

// Use main layout and set Views.
app.useLayout("main");

app.layout.setViews({
".users": new User.Views.List({
collection: this.users
}),

".repos": new Repo.Views.List({
collection: this.repos
}),

".commits": new Commit.Views.List({
collection: this.commits
})
});
app.useLayout().setViews({
".users": new User.Views.List(collections),
".repos": new Repo.Views.List(collections),
".commits": new Commit.Views.List(collections)
}).render();
}
});

Expand Down
30 changes: 0 additions & 30 deletions app/templates/layouts/main.html

This file was deleted.

Loading

0 comments on commit 69c2b34

Please sign in to comment.