我试图了解如何在Backbone模型中处理多个集合实例。将有一个主要模型,该模型由多个集合实例组成。集合本身就是其他模型的组合。
might even be empty but will only add more Model C's
这是非工作代码......
(function() {
var App = {};
window.App = App;
var template = function(name) {
return Mustache.compile($('#'+name+'-template').html());
};
App.World = Backbone.Model.extend({
initialize: function() {
var this.continent = new Array();
this.continent[0] = new App.Continent(0);
this.continent[1] = new App.Continent(1);
this.continent[2] = new App.Continent(2);
this.continent[3] = new App.Continent(3);
this.continent[4] = new App.Continent(4);
this.continent[5] = new App.Continent(5);
}
});
App.Continent = Backbone.Collection.extend({
initialize: function(id) {
switch (id) {
case 0:
this.id = "Europe";
case 1:
this.id = "Asia";
case 2:
this.id = "Africa";
case 3:
this.id = "Australia";
case 4:
this.id = "South America";
default:
this.id = "North America";
}
}
});
App.Index = Backbone.View.extend({
template: template('index'),
initialize: function() {
this.world = new App.World();
this.world.on('all', this.render, this);
},
render: function() {
this.$el.html(this.template(this));
return this;
},
bigland: function() {
return this.world;
},
});
App.Router = Backbone.Router.extend({
initialize: function(options) {
this.el = options.el
},
routes: {
"": "index"
},
index: function() {
var index = new App.Index();
this.el.empty();
this.el.append(index.render().el);
}
});
App.boot = function(container) {
container = $(container);
var router = new App.Router({el: container})
Backbone.history.start();
}
})()
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>Hello world</h1>
<div id='app'>
Loading...
</div>
<script type="text/x-mustache-template" id="index-template">
<ul>
{{#bigland}}
<li>Hello {{.continent}}</li>
{{/bigland}}
</ul>
</script>
<script src="jquery.js"></script>
<script src="underscore.js"></script>
<script src="backbone.js"></script>
<script src="mustache.js"></script>
<script src="app.js"></script>
<script>$(function() { App.boot($('#app')); });</script>
</body>
</html>
答案 0 :(得分:0)
JS Bin链接:
http://jsbin.com/ejohom/1/edit
我只探索了构建模型和集合的不同方法。我创建了一个testData结构,它代表了在初始化时如何加载这些数据。
此外,您将在集合中看到一个大陆探测器功能。