event refactor and errors

This commit is contained in:
Peter Cottle 2012-09-10 12:55:49 -07:00
parent 4f30ad73a8
commit 3d1890bb3b
3 changed files with 111 additions and 36 deletions

View file

@ -28,24 +28,36 @@ var CommandLineView = Backbone.View.extend({
this.submit();
},
processError: function(err) {
// in this demo, every command that's not a git command will
// throw an exception. Some of these errors might be just to
// short-circuit the normal programatic flow, so we handle them
// here
if (err instanceof CommandProcessError) {
events.trigger('commandProcessError', err);
} else if (err instanceof CommandResult) {
events.trigger('commandResultPrint', err);
} else {
throw err;
}
},
submit: function() {
var value = this.$('#commandTextField').val();
this.$('#commandTextField').val('');
events.trigger('commandConsumed', value);
if (!value.length) {
// return early, just want a blank line
}
console.log('the value');
console.log(value);
try {
var command = new Command(value);
console.log(command);
// immediately execute for now TODO
gitEngine.dispatch(command);
} catch (e) {
alert('Error with that command: ' + String(e));
// immediately execute for now, will change later
events.trigger('gitCommandReady', command);
} catch (err) {
if (err instanceof CommandProcessError) {
events.trigger('commandProcessError', err);
} else if (err instanceof CommandResultError) {
} else {
throw err;
}
}
}
});
@ -56,12 +68,26 @@ var CommandLineHistoryView = Backbone.View.extend({
this.addCommand, this
));
events.on('commandProcessError', _.bind(
this.commandError, this
));
events.on('commandResultPrint', _.bind(
this.commandResultPrint, this
));
this.commandTemplate = ' \
<p class="commandLine <%= name %>"> \
<p class="commandLine <%= class %>"> \
<span class="arrows">&gt; &gt; &gt;</span> \
<%= command %> \
</p> \
';
this.resultTemplate = ' \
<p class="commandResult <%= class %>"> \
<%= result %>
</p>
';
},
addCommand: function(commandText) {
@ -74,5 +100,29 @@ var CommandLineHistoryView = Backbone.View.extend({
}
)
);
},
commandError: function(err) {
this.$('#commandDisplay').append(
_.template(
this.resultTemplate,
{
class: 'errorResult',
result: err.toResult()
}
)
);
},
commandResultPrint: function(err) {
this.$('#commandDisplay').append(
_.template(
this.resultTemplate,
{
class; 'commandResult',
result: err.toResult()
}
)
);
}
});