I want to start using Backbone.js to better structure my JavaScript files. However, I don't want to redo my application to ouput just JSON via an API. Correct my if I'm wrong, but so far I have the impression that I can still use Backbone.js, even without a JSON API. Now I'm running into a problem where my server returns HTML and the Backbone model doesn't like that and returns an error.
Basically, I want to load an HTML snippet depending on a category:
var Filter = Backbone.Model.extend({
url: '/filters/',
});
var FilterView = Backbone.View.extend({
initialize: function() {
this.model.on('change', this.updateFilter, this);
this.changeFilter();
},
changeFilter: function() {
this.model.fetch({data: $.param({category: this.options.category})});
},
updateFilter: function(filters) {
console.log(filters);
this.$el.html(filters);
},
});
var filter = new Filter();
var filterView = new FilterView({
el: $( '#filterContainer' ),
category: $( '#categorySlug' ).data( 'slug' ),
model: filter,
});
Now I thought that I can use this simple model to retrieve my HTML snippet via Ajax. The request fires correctly, but Backbone returns an error and updateFilter
is never getting called.
Am I not getting something? What do I need to change to make it work with HTML instead of a JSON response? Or shouldn't I use a model at all?