0
votes

I'm stuck with underscore and backbone. I'm somehow not able to render the JSON within the underscore template. The browser outputs nothing, but there are no error messages. Heres my work:

The server returns the following json:

[{"id":"1","vorname":"Magnus","nachname":"R.","geb":"0","natio":""},{"id":"2","vorname":"Konstantin","nachname":"W.","geb":"0","natio":""}]

///////My Model://////

define([
  'underscore',
  'backbone',
], function(_, Backbone){

  var MitarbeiterModel = Backbone.Model.extend({});
  return MitarbeiterModel;
});

/////My Collection:///////

define([
  'underscore',
  'backbone',
  'models/mitarbeiter',


], function(_, Backbone, MitarbeiterModel){

  var MitarbeiterCollection = Backbone.Collection.extend({
    model: MitarbeiterModel,
    url: '/aquilamus/server/request.php',

  });

  return MitarbeiterCollection;
});

//////My View///////

define([
  'jquery',
  'underscore',
  'backbone',
  'collections/mitarbeiter',
  'text!/aquilamus/templates/mitarbeiter/mitarbeiter.html'
], function($, _, Backbone, MitarbeiterCollection, MitarbeiterTemplate){



 var MitarbeiterListView = Backbone.View.extend({
    el: $("#container"),
    initialize: function(){
      this.collection = new MitarbeiterCollection;

      var newtemplate = MitarbeiterTemplate;
       _.templateSettings.variable = "rc";
      this.template = _.template($(newtemplate).html());
    },
    render: function(){
      var self = this;

      // show some loading message
      this.$el.html('Loading');

      // fetch, when that is done, replace 'Loading' with content
      this.collection.fetch().done(function(){
          console.log(self.collection.toJSON());
          var renderedContent = self.template(self.collection.toJSON());

          self.$el.html(renderedContent);
      });
      return this;
    }


  });
  // Our module now returns our view
  return MitarbeiterListView;
});

Underscore template:

<script type='text/javascript' id='mitarbeiter-anzeigen'>
    <% _.each( rc.mitarbeiter, function(mitarbeiter){ %>
        <div>test</div>
        <div><%= mitarbeiter.vorname %></div>

    <% }); %>   
</script>

The console.log(self.collection.toJSON()) logs the following:

enter image description here

2

2 Answers

2
votes

In your template you have:

<% _.each( rc.mitarbeiter, function(mitarbeiter){ %>

Where did that rc come from?

Here's what you code should look like inside your controller:

self.template({ "mitarbeiters": self.collection.toJSON() });

And then inside your template:

<% _.each( mitarbeiters, function(mitarbeiter){ %>
0
votes

Answer: ///UPDATED underscore template:////

<script type='text/javascript' id='mitarbeiter-anzeigen'>
    <% _.each(rc.mitarbeiters, function(mitarbeiters){ %>
        <div><%= mitarbeiters.vorname %></div>

    <% }); %>   

////UPDATED View:///////

define([
  'jquery',
  'underscore',
  'backbone',
  'collections/mitarbeiter',
  'text!/aquilamus/templates/mitarbeiter/mitarbeiter.html'
], function($, _, Backbone, MitarbeiterCollection, MitarbeiterTemplate){



 var MitarbeiterListView = Backbone.View.extend({
    el: $("#container"),
    initialize: function(){
      this.collection = new MitarbeiterCollection;

      var newtemplate = MitarbeiterTemplate;
       _.templateSettings.variable = "rc";
      this.template = _.template($(newtemplate).html());
    },
    render: function(){
      var self = this;

      // show some loading message
      this.$el.html('Loading');

      // fetch, when that is done, replace 'Loading' with content
      this.collection.fetch().done(function(){
          console.log(self.collection.toJSON());
          var renderedContent = self.template({ "mitarbeiters": self.collection.toJSON() });

          self.$el.html(renderedContent);
      });
      return this;
    }


  });
  // Our module now returns our view
  return MitarbeiterListView;
});