Peter David Carter Peter David Carter - 7 months ago 10
Javascript Question

Alert function in Javascript/Backbone.Marionette

If I'm writing an alert in backbone.marionette how do I start a new line? E.g. when I use the alert() function (I think it's possibly a Javascript function rather than Backbone.Marionette; I'm just using it within Backbone.Marrionette code...) I currently have some code that looks like this:

alert("Some alert text " + variable + " some more alert text");


Which outputs:

"Some alert text [variable] some more alert text"

When I want it to output:

"Some alert text [variable]

Some more alert text"

NOTE: AT THE TIME OF WRITING THIS USER HAD BEGUN USING JAVASCRIPT AND BACKBONE.MARIONETTE AT THE SAME TIME AND WAS UNCLEAR AS TO THE DIFFERENCE. QUESTION LEFT AS IS OTHERWISE, AS THE ACCEPTED ANSWER MAY BE USEFUL.

Answer

This might spark some idea. I will update this answer if you edit your question to make it more specific.

var Input = Backbone.View.extend({
  tagName: 'input',
  initialize: function(options){
    _.bindAll(this, 'onKeydown', 'onKeyup');
    this.$el.attr('type', options.type );
    this.$el.on('keydown', this.onKeydown);
    this.$el.on('keyup', this.onKeyup);
  },
  onKeydown: function(evt){
    /* prevent default action of 'enter' key */
    if( evt.keyCode === 13 ){
      evt.preventDefault();
    }    
  },
  onKeyup: function(evt){
    console.log( 'keyup', evt.keyCode, evt);
    /** if 'return' was pressed */
    if( evt.keyCode === 13 ){
      alert('You Entered: ' + this.$el.val() );
    }
  }
});

var input = new Input({
  type: 'text'
});

input.$el.appendTo( document.body );
<script src='http://code.jquery.com/jquery.js'></script>
<script src='http://underscorejs.org/underscore.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>