My Backbone Underscore Template ReferenceError?

123 Views Asked by At

:: UPDATE 2 ::

This is my current template,

<script type="text/template" id="TimesheetData">  
<form action="#" method="post" id="TimesheetDataList">

<% if(Timesheetrow.jobtitle) { %>

 <div class="TimesheetRowData">
    <input type="hidden" name="data[Timesheetrow][0][id]" value="<%= Timesheetrow.id  %>">
    <input type="type" name="data[Timesheetrow][0][jobtitle]" value="<%= Timesheetrow.jobtitle  %>">
 </div>

<%  }; %>

</form>
<script>

And each jobtitle, is in its own form, not one form (which is what I need) but lots of form tags, each one having one of my job titles

Thanks,


:: UPDATE ::

This is now what my model is

var TimeSheetRowModel = Backbone.Model.extend({
     defaults: {
        Timesheetrow: "",
        Client: "",
        Customer: ""
     }
});

ok, I have most of this working, but for some reason I can not clear out a RefernceError in my console log. However I do (think) I know way its there but not sure how to sort it out.

So my Backbone code :

var TimeSheetRowModel = Backbone.Model.extend({});

var TimeSheetRowCol = Backbone.Collection.extend({
    model: TimeSheetRowModel,
    url: '/dashboard/jsondata/' + GetTimesheetID
});

var NewTimeSheetCollection = new TimeSheetRowCol(); //New Instance Of Collecttion

var TimeSheetView = Backbone.View.extend({
    el:'#testarea', //HTML loading area for the data

    template: _.template( $('#Timesheet-Data').html() ), //Template to load the JSON data into

    initialize: function(){
      this.listenTo(NewTimeSheetCollection, "add", this.AddMyModel);
      NewTimeSheetCollection.fetch();
    },

    AddMyModel: function(TimeSheetRowModel) {  //apply model data to view template and append to view element   
       //console.log( this.$el.append(this.template(TimeSheetRowModel.toJSON()[0])) );
       $(this.el).html(this.template(TimeSheetRowModel.toJSON()));
    }

});

var NewTimeSheetVew = new TimeSheetView(); //New Instance Of The View    
NewTimeSheetVew.render(); //Render Out The View Instance

And my Underscore code :

 <script type="text/template" id="Timesheet-Data">    

       <%= console.log(Client) %>  

 </script>

Now this does console log all my client data, without any problems and I can also each / print this data into my 'testarea' div.

However in my console log I get,

 Uncaught ReferenceError: Client is not defined

Now from what I have read, this seems to be because I am trying to access as an object (?) and toJSON returns a string - please correct my understand if I have that wrong?

But what I do not understand, is if I am returning the wrong type of output, how can I still access the data? and how should I deal with it, how can I make my toJSON return a object?

Thanks,

1

There are 1 best solutions below

10
On BEST ANSWER

Do you have that "Client" property in you model?

eg:

var TimeSheetRowModel = Backbone.Model.extend({

    Client: ""// some value
});

Then only you can access the property in view using underscore.