define([
    'jquery',
    'Underscore',
    'Backbone',
    'models/expense',
    'text!../../tpl/expense-detail.html'
], function ($, _, Backbone, expense, template) {
    var ExpenseDetailsView = Backbone.View.extend({
        self:this,
        initialize:function () {
            this.model.bind("reset", this.render, this);
        },
        events:{
            "click #put":"saveExpense",
            "click #add":"addLine"
        },

        render:function (eventName) {
            var $element=$(_.template(template, this.model.toJSON()));
            _.each(this.model.get('lines'), function(line,index){
                var selectedOption = $element.find('#expenseType').eq(index).children('option[value='+line.expenseType+']');
                selectedOption.attr('selected',true);
            });
            this.setElement($element);
            $('#content').replaceWith($element);
        },

        saveExpense:function () {
            var elements = $('#content td#expense-line');
            var view =this;
            var lines = _.map(elements, function (template) {
                t = $(template);
                return {
                    "expense":(+t.find('#amount').val()),
                    "description":t.find('#description').val(),
                    "valueDate":moment(t.find('#valueDate').val(), window.dateFormat).format(moment.defaultFormat),
                    "expenseType":t.find('#expenseType').val(),
                    "id":null,
                    "expenseReportId":null,
                    "account":"Xebia"
                };
            });
            this.model.set();
            if (this.model.isNew()) {
                this.model.unset('id')
            }
            this.model.save({
                startDate:moment($('#startDate').val(), window.dateFormat).format(),
                endDate:moment($('#endDate').val(), window.dateFormat).format(),
                total:$('#total').val(),
                lines:lines
            });
            return false;
        },
        addLine:function(){
             var table=$("#content table");
            _.each( this.model.defaults.lines, function (line) {
                console.log(line);
                line.valueDate = moment().format(window.dateFormat);
                var el = $(_.template(lineTemplate, line));
                el.find('select option[value=' + line.expenseType + ']').attr('selected', true)
                table.append(el);
            })
        },
        close:function () {
            $(this.el).unbind();
            $(this.el).empty();
        }

    });
    return ExpenseDetailsView;
});

@@ -290,20 +290,18 @@
 s.model.
-bind
+on
 (%22reset%22
@@ -451,16 +451,24 @@
   render
+Template
 :functio
@@ -474,17 +474,12 @@
 on (
-eventName
+json
 ) %7B%0A
@@ -502,17 +502,19 @@
 $element
-=
+ = 
 $(_.temp
@@ -532,29 +532,14 @@
 te, 
-this.model.toJSON(
+json
 ))
-)
 ;%0A  
@@ -559,31 +559,18 @@
 ach(
-this.model.get('
+json.
 lines
-')
 , fu
@@ -579,20 +579,23 @@
 tion
+ 
 (line,
+ 
 index)
+ 
 %7B%0A  
@@ -693,17 +693,19 @@
 %5Bvalue='
-+
+ + 
 line.exp
@@ -712,17 +712,19 @@
 enseType
-+
+ + 
 '%5D');%0A  
@@ -768,16 +768,17 @@
 lected',
+ 
 true);%0A 
@@ -784,32 +784,171 @@
             %7D);%0A
+            return $element;%0A        %7D,%0A%0A        render:function () %7B%0A            var $element = this.renderTemplate(this.model.toJSON());%0A
             this
@@ -1145,16 +1145,17 @@
 r view =
+ 
 this;%0A  
@@ -1426,23 +1426,16 @@
 ueDate%22:
-moment(
 t.find('
@@ -1457,57 +1457,8 @@
 l(),
- window.dateFormat).format(moment.defaultFormat),
 %0A   
@@ -1839,31 +1839,24 @@
   startDate:
-moment(
 $('#startDat
@@ -1869,37 +1869,8 @@
 l(),
- window.dateFormat).format(),
 %0A   
@@ -1890,23 +1890,16 @@
 endDate:
-moment(
 $('#endD
@@ -1914,37 +1914,8 @@
 l(),
- window.dateFormat).format(),
 %0A   
@@ -2061,13 +2061,14 @@
 tion
+ 
 ()
+ 
 %7B%0A
- 
     
@@ -2083,14 +2083,17 @@
 var 
+$
 table
-=
+ = 
 $(%22#
@@ -2125,369 +2125,124 @@
     
-_.each( this.model.defaults.lines, function (line) %7B%0A                console.log(line);%0A                line.valueDate = moment().format(window.dateFormat);%0A                var el = $(_.template(lineTemplate, line));%0A                el.find('select option%5Bvalue=' + line.expenseType + '%5D').attr('selected', true)%0A                table.append(el);%0A            %7D)
+var $element = this.renderTemplate(this.model.defaults);%0A            $table.append($element.find('table tbody tr'));
 %0A   

Fix add new expense line