简体   繁体   English

如何在Backbone.js中的Router.routes中绑定外部方法

[英]How to bind external methods in Router.routes in Backbone.js

Is it possible to bind methods from other object in the Router.routes map? 是否可以绑定Router.routes映射中其他对象的方法?

I have all the "logic code" inside of Views and I would like to avoid define methods in the Router . 我在Views中有所有“逻辑代码”,我想避免在路由器中定义方法。

For instance, I have this: 例如,我有这个:

var app = new (Backbone.View.extend({    
         playHome: function(){
            // logic code here
         }
//..........
});
var router = new (Backbone.Router.extend({
        routes: {
          "play"         : 'playHome'
        },
        playHome: function(){
            app.playHome();
        }

I woud like to do something like this: 我想做这样的事情:

var router = new (Backbone.Router.extend({
        routes: {
          "play"         : 'app.playHome'
        },

thanks in advance 提前致谢

Here's a fairly easy way to accomplish what you want: 这是一个很容易实现你想要的方法:

  var WorkspaceRouter = Backbone.Router.extend({
    routes: {
      "help": "help", // #help
      "search/:query": "search", // #search/kiwis
      "search/:query/p:page": "search" // #search/kiwis/p7
    },

    help : function () { },

    search : function () { }
  });

  var App = {
    help: function() { console.log("help"); },

    search: function(query, page) { console.log("search", query, page); }
  };

  $(document).ready(
      function () {
        var router = new WorkspaceRouter();
        Backbone.history.start();

        router.on("route:help", App.help);
        router.on("route:search", App.search);
      }
  );

This solution relies on the fact that Backbone fires an event for each route as well as calling a function on the router itself. 此解决方案依赖于Backbone为每个路由触发事件以及在路由器本身上调用函数的事实。 If you're willing to allow the dummy functions on the router that do nothing then you can attach whatever you like to the router's events and call any number of other functions on other objects, attaching the whole thing (or detaching it) as needed. 如果你愿意允许路由器上的虚拟功能什么都不做,那么你可以将任何你喜欢的东西附加到路由器的事件上,并调用其他对象上的任意数量的其他功能,根据需要附加整个东西(或分离它)。

查看Backbone.Marionette的AppRouter ..它允许您提供一个控制器对象,它必须提供所需的路由方法。

Here's another way to accomplish what you're looking for... 这是完成你所寻找的另一种方式......

var app = new (Backbone.View.extend({    
    playHome: function(){
        // logic code here
    }
    //..........
});

var router = new (Backbone.Router.extend({
    initialize: function(){
        // route,  route name for event, callback function
        this.route("play", "playRoute", _.bind(app.playHome, app))
    },

It's calling app.playHome as the callback function for the route, with app bound as this for the callback. 它调用的app.playHome作为路由的回调函数,与app绑定为this回调。

I think I read this somewhere, where? 我想我在某个地方看过这个,在哪里? no idea. 不知道。 But you can pass first-class functions as values to the routes object. 但是您可以将第一类函数作为值传递给routes对象。 This way: 这条路:

var router = new (Backbone.Router.extend({
    routes: {
      "play"         : app.playHome
    },

No guarantees though! 虽然没有保证! Wonder where I read this. 不知道我在哪里读到这个。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM