Permalink
Browse files

Write stage changes back to GitHub

I fought with Backbone and GitHub's not-quite-RESTful `issue/labels` endpoint
for quite a while, but ultimately direct jQuery was the easier way to go. It's a
shame to have nonuniform code talking to their API, but using Backbone was long,
hackey and buggy, so I'll settle for short, unconventional and functional.
  • Loading branch information...
1 parent 6c3bad3 commit 5d420b839c4b09041522a3e14b5996173a9ef7f6 @kristjan committed Apr 11, 2012
Showing with 62 additions and 17 deletions.
  1. +16 −3 public/js/githud/kanban.js
  2. +46 −14 public/js/githud/models.js
@@ -63,9 +63,21 @@ GitHUD.Kanban = (function() {
}
}
+ function refreshStages() {
+ stageViews.each(function(view) {
+ view.render();
+ });
+ }
+
function moveIssue(issue, from, to) {
- from.get('issues').remove(issue);
- to.get('issues').add(issue);
+ var that = this;
+ issue.changeStage(from, to, {
+ success: function() {
+ from.get('issues').remove(issue);
+ to.get('issues').add(issue);
+ },
+ error: GitHUD.Kanban.refreshStages
+ });
}
function generateIssueFetchers(repos) {
@@ -82,7 +94,8 @@ GitHUD.Kanban = (function() {
return {
init : init,
- moveIssue : moveIssue
+ moveIssue : moveIssue,
+ refreshStages : refreshStages
};
})();
@@ -1,5 +1,10 @@
if (typeof GitHUD === 'undefined') GitHUD = {};
+Backbone.origSync = Backbone.sync;
+Backbone.sync = function(method, model, options) {
+ return Backbone.origSync(method, model, options);
+};
+
$(function() {
GitHUD.Repo = Backbone.Model.extend({
initialize: function(handle) {
@@ -21,12 +26,9 @@ $(function() {
success: done
});
},
- labels: function() {
- return new GitHUD.Labels([], {repo: this});
- },
- url: function() {
- return GitHUD.Util.url('repos/' + this.get('handle'));
- }
+ labels: function() { return new GitHUD.Labels([], {repo: this}); },
+ path: function() { return 'repos/' + this.get('handle'); },
+ url: function() { return GitHUD.Util.url(this.path()); }
});
GitHUD.Repos = Backbone.Collection.extend({});
@@ -36,16 +38,34 @@ $(function() {
GitHUD.Util.initRepo(this, options);
this.id = this.slug();
},
+ changeStage: function(from, to, callbacks) {
+ var oldLabel = new GitHUD.Label({issue: this, name: from.get('name')});
+ var labels = new GitHUD.Labels([], {issue: this});
+ $.post(
+ labels.url(),
+ JSON.stringify([to.get('name')]),
+ function(data, status, xhr) {
+ $.ajax({
+ type: 'DELETE',
+ url: oldLabel.url(),
+ success: callbacks.success,
+ dataType: 'json'
+ }).error(callbacks.error);
+ }
+ ).error(callbacks.error);
+ },
slug: function() {
return GitHUD.Util.slug('issue',
this.get('repo').get('handle') +
'#' + this.get('number'));
},
+ path: function() {
+ return 'repos/' + this.get('repo').get('handle') +
+ '/issues/' + this.get('number');
+ },
url: function() {
return GitHUD.Util.url(
- this.get('url') ||
- ('repos/' + this.get('repo').get('handle') +
- '/issues/' + this.get('number'))
+ this.get('url') || this.path()
);
}
});
@@ -67,28 +87,40 @@ $(function() {
});
GitHUD.Label = Backbone.Model.extend({
- idAttribute: 'name',
initialize: function(options) {
GitHUD.Util.initRepo(this, options);
- this.set('issues', new GitHUD.LabelIssues([]));
+ this.issue = options.issue;
+ this.set('issues', new GitHUD.LabelIssues([], {issue: this.issue}));
this.id = this.slug();
},
slug: function() {
return GitHUD.Util.slug('label', this.get('name'));
},
+ path: function() {
+ var name = encodeURIComponent(this.get('name'));
+ if (this.collection) {
+ return this.collection.path() + '/' + name;
+ }
+ var base = this.get('issue') || this.get('repo');
+ return base.path() + '/labels' + '/' + name;
+ },
url: function() {
- return GitHUD.Util.url('repos/' + this.get('repo').get('handle') +
- '/labels/' + this.get('name'));
+ return GitHUD.Util.url(this.path());
}
});
GitHUD.Labels = Backbone.Collection.extend({
model: GitHUD.Label,
initialize: function(models, options) {
GitHUD.Util.initRepo(this, options);
+ if (options) this.issue = options.issue;
+ },
+ path: function(){
+ var base = this.issue || this.repo;
+ return base.path()+ '/labels';
},
url: function() {
- return GitHUD.Util.url('repos/' + this.repo.get('handle') + '/labels');
+ return GitHUD.Util.url(this.path());
}
});

0 comments on commit 5d420b8

Please sign in to comment.