所以我一直在玩 Backbone Marionette,并且能够从 json 数据创建一个集合视图。
这是我所拥有的图片
,
我试图让每个 itemview 都有一个 onclick 事件,该事件将打开一个嵌套视图,该视图通过下划线模板从 itemview 传递数据。这是一个 swapview 示例(不是我的顺便说一句):http//jsfiddle.net/VLY4t/14/
这是我的代码的样子(第二张图片)(请注意,我有一个通过 url 散列的骨干路由器,但无法呈现下划线模板)。非常感谢任何帮助。:工作示例是:狗的品种是 <%= name %>
客户端模板和 UI 是这样的:
<script type="text/template", id="dogs-template">
<ul><ul>
</script>
<script type="text/template", id="dog-template">
<a href="dogs/<%= name %>"> <%= name %>
</script>
<script type="text/template", id="play-template">
p This dog's breed is <%= name %>
</script>
Javascript是:
//Im serving data via RESTful JSON, but the data looks like this
var dogs = [{name: 'yorkie'}, {name: 'pitbull'}, {name: 'dobberman'}, etc]
App = new Backbone.Marionette.Application();
App.addRegions({
mainRegion: "#content",
playRegion: "#play"
});
//Call the backbone history here
App.on("initialize:after", function(options){
if (Backbone.history){
Backbone.history.start({pushState: true});
}
});
//Call the Controller
Controller = {
};
// Start the models n collections
Dog = Backbone.Model.extend({
});
Dogs = Backbone.Collection.extend({
model: Dog,
url: '/jonas',
});
DogView = Backbone.Marionette.ItemView.extend({
template: "#dog-template",
tagName: 'li',
initialize: function(){
//var moot = _.bindAll(this.model);
},
events: {
"click" : function() {
//show new region here
App.playRegion.show(theplayview);
}
}
});
PlayView = Backbone.Marionette.ItemView.extend({
template: '#play-template'
});
DogsView = Backbone.Marionette.CollectionView.extend({
template: "#dogs-template",
itemView: DogView
})
var doggies = new Dogs();
var bob = doggies.fetch({async: false});
var doggyview = new DogsView({collection: doggies});
App.mainRegion.show(doggyview);
var theplayview = new PlayView(this.model);
MyRouter = Backbone.Marionette.AppRouter.extend({
controller: Controller,
appRoutes: {
"": "route1",
"testrouter": "route2",
"testrouter#dogs": "route3",
"testrouter#dogs/:id": "route4",
},
});
App.addInitializer(function(){
console.log("Router is on")
new MyRouter();
});
App.on("initialize:after", function(){
});
App.start();