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: