 var editor = CodeMirror.fromTextArea(document.getElementById("templateEditor"), { lineNumbers: true, lineWrapping: true });
 editor.setSize(800, 500);
 $(function() {
     $("#save").on('click', function(e) {
         e.preventDefault();
         var id = $("#id").val();
         var name = $("#name").val();
         var content = editor.getDoc().getValue();
         if (name == "") {
             alert('Please Enter A Name');
         } else {
             $.ajax({
                 type: 'POST',
                 url: '/api/templates/' + id + '/save',
                 dataType: 'json',
                 data: JSON.stringify({ "content": content, "name": name }),
                 contentType: 'application/json',
                 complete: function(data) {
                     // do something
                     $('#results').text("Successfully Saved, refresh page to see changes");
                     //setInterval(function() { location.reload() }, 1000);
                     //Session.set("result", "Successfully Saved, refresh page to see changes");
                     //Session.set("tempId", id);
                     //setInterval(function () {
                     //location.reload();
                     //}, 2000);
                 }
             });
         }
     });

     $("#name").on('click', function(e) {
         e.preventDefault();
     });

     $("#new").on('click', function(e) {
         //e.preventDefault();
         //get count of templates + 1
         var id = $("#tempMenu").find("li").length;
         $("#id").val(id);
         var name = $("#name").val("");
         //var content = editor.getDoc().getValue();
         editor.getDoc().setValue("");
         var content = $('#templateEditor').val("");
     });

     $("#delete").on('click', function(e) {
         e.preventDefault();
         var id = $("#id").val();
         if (id != "") {
             $.ajax({
                 type: 'DELETE',
                 url: '/api/templates/' + id,
                 dataType: 'json',
                 data: JSON.stringify({ "id": id }),
                 contentType: 'application/json',
                 complete: function(data) {
                     // do something
                     $('#results').text("Successfully Deleted, reload page to see changes");
                     //var results = "Successfully Deleted";
                     //Session.set(result, "Successfully Deleted");
                     //setInterval(function () {
                     //location.reload()
                     //}, 2000);
                 }
             });
         } else { alert("Can't delete an empty template"); }
     });

     /*$(".leaf").on('click', function(e) {
         var id = this.id.replace("temp_", "");
         var name = this.textContent;
         $.ajax({
             type: 'GET',
             url: '/api/templates/' + id + '',
             //dataType: 'json',
             //data: JSON.stringify(emptyDevice),
             //contentType: 'application/json',
             complete: function(data) {
                 //$('#templateEditor').val(data.responseText);
                 editor.getDoc().setValue(data.responseText);
                 $('#name').val(name);
                 $('#id').val(id);
             }
         });
     });*/

 });

 $(document).ready(function() {
     var id = $('#id').val();
     if (id != "") {
         $.getJSON('/api/templates/' + id + '', function(data) {
             //$('#templateEditor').val(data.responseText);
             editor.getDoc().setValue(data.css);
             $('#name').val(data.name);
             //$('#id').val(id);

         });
     }
 });
@@ -258,32 +258,120 @@
 $(%22#id%22).val();%0A
+         if (id == %22%22) %7B%0A             id = $('#tempMenu').find('li').length;%0A         %7D%0A
          var nam
@@ -384,32 +384,32 @@
 %22#name%22).val();%0A
-
          var con
@@ -1314,32 +1314,91 @@
     //%7D, 2000);%0A
+                     window.location = %22/templates/%22 + id;%0A
                 
@@ -1889,24 +1889,65 @@
 ').val(%22%22);%0A
+         window.location = %22/templates%22;%0A
      %7D);%0A%0A  
@@ -2708,16 +2708,16 @@
 eload()%0A
-
         
@@ -2733,32 +2733,85 @@
     //%7D, 2000);%0A
+                     window.location = %22/templates%22;%0A
                 

fix for new template redirect