Skip to content

Instantly share code, notes, and snippets.

@fivetanley
Created September 22, 2012 19:02
Show Gist options
  • Save fivetanley/3767420 to your computer and use it in GitHub Desktop.
Save fivetanley/3767420 to your computer and use it in GitHub Desktop.
repl view
define( function( require ) {
var Backbone = require( 'backbone' )
, _ = require( 'underscore' )
, $ = require( 'jquery' )
function createListItem( content, domClass) {
return $( '<li class="' + domClass + '">' + content + '</li>' )
}
function decorate( input ) {
switch ( typeof input ) {
case 'undefined': return 'undefined'
case 'string': return '\"' + input + '\"'
}
return input
}
return Backbone.View.extend({
initialize: function( options ) {
_.bindAll( this, 'handleSubmit' )
this.repl = this.model
this.$input = this.$el.find( '[name=stdin]' )
this.$replResults = this.$el.find( '.repl_results' )
}
, events: {
'submit': 'handleSubmit'
}
, handleSubmit: function( event ) {
event.preventDefault()
var input = this.$input.val()
this.$replResults.append( createListItem( input, 'input' ) )
try {
var result = decorate( this.repl.$eval( input ) )
this.$replResults.append( createListItem( result, 'result' ) )
} catch ( error ) {
this.$replResults.append( createListItem( error.toString(), 'exception' ) )
}
this.$input.val( '' )
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment