我試圖在後端使用Django製作Backbone Application。我正在關注Backbone教程。我用下面的代碼:Backbone Collection
代碼
<!doctype html>
<html lang = "en">
<meta charset = "utf-8">
<title>IstreetApp</title>
<link rel="stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Book Manager</h1>
<hr />
<div class="page"></div>
</div>
<script type = "text/template" id = "booklist.template">
</script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.10/backbone-min.js"></script>
<script>
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
options.url = 'http://backbonejs-beginner.herokuapp.com' + options.url;
});
var Books = Backbone.Collection.extend({
url: '/books'
});
var BookList = Backbone.View.extend({
el: '.page',
render: function() {
var that = this;
var books = new Books();
books.fetch({
success: function(books) {
var template = _.template($('#booklist.template').html(), {books: books.models});
that.$el.html(template);
}
})
}
});
var Router = Backbone.Router.extend({
routes: {
'': 'home'
}
})
var bookList = new BookList();
var router = new Router();
router.on('route:home', function() {
bookList.render();
});
Backbone.history.start();
</script>
</body>
</html>
因爲收藏品都是沒有定義,成功的代碼不執行。我認爲收集數據應該來自服務器通過Django,但我不知道如何以及以何種形式。請幫助。我對骨幹和Django來說非常新穎。
爲什麼不在你的_fetch_調用中放置一個錯誤處理程序? – snedkov 2013-03-16 17:55:28
@svetoslavnedkov我該怎麼做? – 2013-03-16 18:08:17
它使用與成功處理程序相同的格式,只需在成功處理程序後面放置',error:function(data){alert(data);}'即可。 – snedkov 2013-03-16 18:14:48