要引导您对数据建模,您只需要将模型 JSON 呈现到标签中。从主干模式链接释义:
<script>
// app data, rendered on server
var photosJSON = [
{ id: 2, name: "My dog", filename: "IMG_0392.jpg" },
{ id: 3, name: "Our house", filename: "IMG_0393.jpg" },
{ id: 4, name: "My favorite food", filename: "IMG_0394.jpg" },
{ id: 5, name: "His bag", filename: "IMG_0394.jpg" },
];
</script>
...
<script>
var Photo = Backbone.Model.extend({
// photo attributes (from above)
});
var PhotoCollection = Backbone.Collection.extend({
model:Photo
});
// build collection model without AJAX call using
// server rendered JSON above
var photosCollection = new Photos(photosJSON);
</script>
您可以在服务器上使用任何适当的技术来呈现带有 JSON 数据的脚本。