Reputation: 10608
I have a canonical model with an associated view:
var Item = Backbone.Model.extend({
url: function() {
return "/item/123"
}
});
var ItemView = Backbone.View.extend({});
However, on the server-side, at url "/item/123", my Django application does not render JSON-formatted content, but an HTML template that is designed to be directly inserted inside the main page. How can I render the model without drastically changing how my server serves dynamic content? Is it even possible or am I misunderstanding the whole philosophy behind Backbone.js?
Just to give you a little background: I am in the process of refactoring the JS code of a Django web application by integrating Backbone.js. The app itself is not very large, but it makes heavy use of Ajax calls.
Upvotes: 1
Views: 194
Reputation: 12431
I don't think you should be loading templates in a model
. The loading and rendering of templates would usually be the job of the view
. Try loading the HTML directly with AJAX in the render
method of your view
:
var ItemView = Backbone.View.extend({
render: function(){
var that = this;
$.get('/item/123', function(html){
that.$el.html(html);
});
return this;
}
});
Upvotes: 2
Reputation: 4154
If you havent already, have a look at the django-app Tastypie, its the go-to solution for backend to backbone and similar apps.
Without seing the code its hard to tell how much work it would be to move it all into Tastypie vs rolling your own solutions on a per-case basis, look over the documentation.
Upvotes: 0