似乎无法在backbone.js中呈现主视图

时间:2014-04-09 18:16:48

标签: javascript backbone.js yeoman yeoman-generator

我在Backbone.js中比较新,并且正在尝试构建一个用于学习的应用程序。我使用过Yeoman骨干生成器,在尝试渲染主视图时,不断收到错误

TypeError: Object [object Object] has no method 'template' 

这是我的main.js

 App = {
    Models: {},
    Collections: {},
    Views: {},
    Routers: {},
    Adapters:{}

};

$(document).ready(function () {
    'use strict';
    App.routers = new App.Routers.MainRouter();
    Backbone.history.start();

});

这是我的MainRouter.js

App.Routers = App.Routers || {};
    'use strict';
    App.Routers.MainRouter = Backbone.Router.extend({
        routes: {
        "": "index",
        "buildings/:id" : "buildingDetails",
        "buildings/:id/map": "map" 
    },

    initialize: function(){
        App.slider = new PageSlider($('body'));
    },

    index: function(){
        if(!App.mainView){  
            App.mainView = new App.Views.MainView();
            console.log(App.mainView)
            App.mainView.render();
             }else {
             App.mainView.delegateEvents();
            }
            App.slider.slidePage(App.mainView.$el);
        },

buildingDetails: function(id){
        var building = new  App.Models.BuildingModel({id: id});
            building.fetch({
            success: function(data){
                var buildingView = new App.Views.BuildingView({model: data});
                App.slider.slidePage(buildingView.render().$el);
            },
            error: function(response){
                console.log(error,'There was some error in accessing data');
            }

        });
    },

这是我的BuildingListView.js

App.Views = App.Views || {};
    App.Views.BuildingListView = Backbone.View.extend({
        tagName: 'ul',
        template: JST['app/scripts/templates/BuildingListView.ejs'],
        attributes: {class: 'topcoat-list list'},

        initialize: function(){
            this.model.on("reset", this.render, this);
        },

        render: function(){
            this.$el.empty();
            _.each(this.model.models, function(building){
            App.singleBuildingListView = new App.Views.SingleBuildingListView({model: building});
                console.log(App.singleBuildingListView);
                this.$el.append(App.singleBuildingListView.render().el);
            }, this);
            return this;
        }
    });

    App.Views.SingleBuildingListView = Backbone.View.extend({
        tagName: "li",
        className: "topcoat-list__item",

        initialize: function(){
            this.model.on("change", this.render, this);
            this.model.on("destroy", this.close, this);
        },

        render: function(){
            var html = this.template(this.model.attributes);
            this.$el.html(html);
            return this;
        }
    });

这是我的MainView.js

App.Views = App.Views || {};

    App.Views.MainView = Backbone.View.extend({

        template: JST['app/scripts/templates/MainView.ejs'],

        initialize: function(){
            className: '.scroller',
            this.buildingList = new App.Models.BuildingCollection();
            this.buildingList.fetch({reset: true, data:{name:""}});
            this.buildingListView = new App.Views.BuildingListView({model: this.buildingList});
        },

        render: function(){
            this.$el.html(this.template());
            this.$el.append(this.buildingListView.render().el);
            return this;
        } });

每当我导航到特定的建筑物时,就会发生这种情况。 http://localhost:8000/#buildings/1http://localhost:8000/#buildings/1/map,一切正常,甚至地图也已加载。但每当我导航到mainview本身时,它都不起作用,我似乎无法弄清楚原因。我认为主视图似乎有问题,但我不能弄明白。对此问题的任何帮助或建议将不胜感激,谢谢。这个应用程序的完整代码avialabel https://github.com/khyamay/uopTourGuide

1 个答案:

答案 0 :(得分:0)

SingleBuildingListView使用this.template,但您未在视图中指定模板。