BackboneJS - 路由器功能 - 调用视图?

时间:2013-09-17 22:53:47

标签: javascript json backbone.js

我在初始化时使用路由器cfunction调用我的视图时遇到问题。

这是我的路由器代码:

var ApplicationRouter = Backbone.Router.extend({
        routes: {
                "": "home",
               //"*actions": "home",
                "photo-gallery": "gallery",
                "sound-lounge": "sound",
                "contact": "contact"
        },
        initialize: function() {
               this.homeView = new window.app.HomeView();
               this.homeView.render();

               /*
               var soundView = new window.app.SoundView();
                soundView.render();

                var contactView = new window.app.ContactView();
                contactView.render();

                var galleryCollection = new window.app.GalleryCollection();
        var gallery_items = galleryCollection.fetch();
         gallery_items.done(function(){
           var gallery_item = new GalleryView({ collection: galleryCollection });
         });
         */



        },
        home: function() {

                console.log('home');
        },
        sound: function() {
               //var soundView = new window.app.SoundView();
                //soundView.render();
                console.log('sound');
        },
         contact: function() {
               //var contactView = new window.app.ContactView();
               // contactView.render();
                console.log('contact');
        },
        gallery: function() {

            console.log('gallery');

        }
});

你可以看到我有:

var soundView = new window.app.SoundView();
soundView.render();

在initalize函数中注释掉,这会有效但是只要我把它放在'sound'函数中我就会收到这个错误:未捕获的TypeError:undefined不是函数

这与这部分有关:new window.app.SoundView();

我的代码中有window.app.SoundView,如何让它工作(我的所有部分都不正确)

//添加了soundView代码

    // SOUNDVIEW    
window.app.SoundView = Backbone.View.extend({
    el: $("html"),
    events: {
      "click #sound-lounge": "render_sound"
    },
    initialize: function(){
        this.render_sound();
    },
    render: function(model){
        return this;
    },
    render_sound: function(){
        this.model=new window.app.Home({id: 2});
        var $main=this.$el.find('#content-area');
        this.model.fetch().complete(function(data){
            $main.html(data["responseJSON"].description);
        });
    }
});

由于

1 个答案:

答案 0 :(得分:0)

我的问题最终变得像删除window.app一样简单。来自soundView变量名。

e.g。

window.app.SoundView = Backbone.View.extend({== SoundView = Backbone.View.extend({

var soundView = new window.app.SoundView(); == var soundView = new SoundView();