我怎么能摆脱:TypeError:communityApp.activeTabLayout.pforum是未定义的

时间:2014-02-20 05:33:32

标签: backbone.js

我的模块代码是。

spine.module('communityApp',{     startWithParent:false,     define:function(communityApp,App,Backbone,Marionette,$,_){         的console.log( “communityApp.js”);

    // Setup the router for this module
    var Router = Marionette.AppRouter.extend({
        before: function () {
            console.log("communityApp.js: router.before()")
            App.startSubApp("communityApp", {});
        },
        appRoutes: {
            "community": "showCommunityTab",
            "community/pforum": "getPforum",
            "community/questions":"getQuestions",
            "community/events": "getEvents"
        }
    });

    // Startup router
    App.addInitializer(function () {
        console.log("communityApp.js: App.addInitializer()")
        // contains active controller
        communityApp.activeController = new communityApp.Controllers.tabController();
        // initializing route
        communityApp.Router = new Router({
            controller: communityApp.activeController
        });
    });

    // Let app know we started
    communityApp.addInitializer(function () {
        console.log("communityApp.js: DemoApp.addInitializer()");
        App.vent.trigger("app:started", "communityApp");
    });

    // This will run when a sub app (module) starts
    communityApp.on("start", function () {
        console.log("communityApp.js: on 'Start'()");
    });

控制器代码是。

spine.module(“communityApp”,功能(communityApp,App,Backbone,Marionette,$,_){     “使用严格”;

// initializing controllers and collections for message tabs
communityApp.Controllers = {};
communityApp.Collections = {};

communityApp.Controllers.tabController = Marionette.Controller.extend({
    showCommunityTab: function () {
        this.getCommunityTab();
    },
    getCommunityTab: function (data) {
        //var tabLayout = new communityApp.Views.tabLayout();
        //tabLayout.render();
        // creating active layout
        communityApp.activeTabLayout = new communityApp.Views.tabLayout();
        communityApp.activeTabLayout.render();
        // loading community module view
        App.regionMain.show(communityApp.activeTabLayout);
        // load pforum on community module load
        this.getPforum();
    },
    getPforum : function(){
        console.log('Public Forum Tab');
        var pforum = new communityApp.Controllers.pforumController();
        pforum.init();

    },
    getQuestions : function(){
        console.log('Question tab');
        var questions = new communityApp.Controllers.questionsController();
        questions.init();

    },
    getEvents : function(){
        console.log('Events tab');
        var events = new communityApp.Controllers.eventController();
        events.init();

    }
});

错误所在的代码,它是一个标签页。

spine.module(“communityApp”,功能(communityApp,App,Backbone,Marionette,$,_){     “使用严格”;

communityApp.Controllers.pforumController = Marionette.Controller.extend({
    init: function(){
        var func = _.bind(this._getPforum, this);
        $.when(App.request('alerts1:entities' , {origin:'pforum'}))
            .then(func)
    },
    _getPforum:function(data){

        // populating the data
        communityApp.activeTabLayout.pforum.show(new communityApp.CollectionViews.pforumCollectionViews({
            collection: data
        }));
    }
});

1 个答案:

答案 0 :(得分:0)

那是因为您在activeTabLayout中创建了getCommunityTab,并且您在getPforum中创建控制器之后尝试访问它。这些都附加到不同的路线。因此,您communityApp.activeTabLayout方法中init存在community/pforum并不保证。

community - >创建您的控制器,但不是您的标签布局。

communityApp.activeTabLayout = new communityApp.Views.tabLayout(); - >创建标签布局,但不是您的控制器。

您需要确保在pforum.init();

之前运行{{1}}