Backbone Marionette:从CollectionView获取嵌套/交换视图

时间:2013-04-09 05:02:10

标签: backbone.js marionette

所以我一直在玩Backbone Marionette并且能够从json数据创建一个collectionview。

这是我拥有的图片

The CollectionView

我试图让每个itemview都有一个onclick事件,它将打开一个嵌套视图,通过下划线模板从itemview传递数据。 这是一个swapview示例(不是我的btw):http // jsfiddle.net / VLY4t / 14 /

这是我的代码看起来像(第二张图片)(注意我有一个url hash的骨干路由器,但无法呈现下划线模板)。任何帮助是极大的赞赏。: 工作实例将是:狗的品种是<%= name%> Child from Itemview

客户端模板和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();

1 个答案:

答案 0 :(得分:4)

尝试连接点击处理程序,如此

events: {
 "click" : "showView"
},
showView: function() {
  var theplayview = new PlayView(this.model);
  App.playRegion.show(theplayview); 
}