2014-02-11 79 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