Created
September 22, 2012 19:02
-
-
Save fivetanley/3767420 to your computer and use it in GitHub Desktop.
repl view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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