How to automatically pick a "mode" for Ace Editor, given a file extension

Ace now provides modelist extension to do this.

var modelist = ace.require("ace/ext/modelist")
var filePath = "blahblah/weee/some.js"
var mode = modelist.getModeForPath(filePath).mode
editor.session.setMode(mode) // mode now contains "ace/mode/javascript".

Note that if you are using prebuilt version of ace you need to include ace.js and ext-modelist.js files in your page.
With source version, you need to replace ace.require with require and require.js will load all dependencies automatically.

See https://github.com/ajaxorg/ace/blob/master/demo/modelist.html and https://github.com/ajaxorg/ace-builds/blob/master/demo/modelist.html for examples of how to use it