2014-02-11 117 views
0

我正在使用Backbone + requirejs構建我的應用程序,在下面的模塊中,initialize函數用於從另一個稱爲「HomeView,InnerView」的模塊中加載初始化函數,我將喜歡傾聽Backbone.history事件。但該模塊不聽這種事件。骨幹監聽Backbone.history事件

路由器

// Filename: router.js 
define([ 
'jquery', 
'underscore', 
'backbone', 
'app/config', 
'app/views/homeview', 
'app/views/inner_view' 
], function($, _, Backbone, Config, HomeView, InnerView) { 

var AppRouter = Backbone.Router.extend({ 

    initialize: function() { 

    }, 
    routes: { 
     // Define some URL routes 
     '': 'defaultRoute', 
     '!/home': 'defaultRoute', 
     '!/activities': 'activities', 
     '!/activities/:id': 'activity', 
     '!/vacancies': 'vacancies', 
     '!/vacancies/:id': 'vacancies', 
     // 404, not found 
     '*actions': '404' 
    }, 
    defaultRoute: function() { 

     //if this route is served from inner pages, we have to trigger "close:Inner" 
     Backbone.trigger('close:Inner'); 
     Backbone.current = 'Home'; 

    }, 
    activities: function(){ 

     Backbone.trigger('view:Activities'); 
     Backbone.trigger('close:'+Backbone.current);    
     Backbone.current = 'Activities';    

    }, 
    activity: function(id){ 

     Backbone.trigger('close:' + Backbone.current); 
     Backbone.current = 'Activity'; 

    }, 
    vacancies: function(){ 

    },  
    404: function(actions) { 

     // We have no matching route, lets display 404 
     $('.content_wapper').html(actions + " no such page on the system"); 

    } 

}); 

var initialize = function() { 

    HomeView.initialize(); 
    InnerView.initialize();     

    Backbone.appRouter = new AppRouter(); 
    Backbone.history.start(); 

}; 

return { 
    initialize: initialize 
}; 
}); 

什麼是錯的是防止我聽backbone.history的另一視圖中的事件?

Innerview模塊

// Filename: views/boilerplate.js 
define([ 
'jquery', 
'underscore', 
'backbone', 
'app/config', 
'text!templates/inner_header.html', 
'bootbox', 
'text!templates/breadcrumb.html', 
'purl' 
], function($, _, Backbone, Config, inner_header_template, bootbox, breadcrumbTemplate) { 

var url = $.url(), 
    InnerView = Backbone.View.extend({ 

    el: $(".header_content"), 
    template: _.template(inner_header_template), 
    initialize: function(options) { 

     Backbone.history.on('route:activities', this.activities);   

    }, 
    render: function() {}, 
    activities: function() { 
     alert('activities'); 
    }, 
    events: {}, 
    close: function() { 

     $('.top_menu').remove(); 
     $('.main_menu_inner').remove(); 
     $('.content_container').empty(); 
     $(this.el).unbind(); 
     delete this.$el; //delete the jQuery wrapped object variable 
     delete this.el; //delete the variable reference to this node 

    } 

}); 

var initialize = function() { 

    new InnerView(); 

}; 

return { 
    initialize: initialize 
}; 


}); 

回答

0

是觸發骨幹事件對象不Backbone.history,試試這個:

Backbone.history.trigger('close:Inner'); 

你必須聽路由器本身上不在Backbone.history上。 Here is an example,說明這一點。

+0

我在詢問這個事件'Backbone.history.on('route:activities',this.activities);'你提到的事件對我來說完美無缺:) – ahmedsaber111

+0

你在哪裏觸發了'route:activities'事件? –

+0

在初始化爲路由器模塊的初始化函數的InnerView內部 – ahmedsaber111