0

私は次のように定義されたルーターを持っています:

var MyRouter = Backbone.Router.extend({
  routes: {
    // catch-all for undefined routes
    "*notfound" : "notFound",
  },

  initialize: function(options) {
    this.route("my_resource/clear_filters", 'clearFilters');
    this.route("my_resource/:id", 'show');
  },

  show: function(id){
    console.log('show', id);
  },

  clearFilters: function() {
    console.log('clearFilters');
  },

  notFound: function() {
    console.log('notFound');
  },
});

var app = {};
app.MyRouter = new MyRouter();
Backbone.history.start({silent: true});

したがって、次の URL は次のようにマップされます。

var opts = {trigger: true};
app.MyRouter.navigate('/foo', opts);                       // logged -> 'notFound'
app.MyRouter.navigate('/my_resource/123', opts);           // logged -> 'show', '123'
app.MyRouter.navigate('/my_resource/clear_filters', opts); // logged -> 'clearFilters'
app.MyRouter.navigate('/my_resource/some_thing', opts);    // logged -> 'show', 'some_thing'

で処理されるmy_resource/:idように、数値パラメータのみに一致するようにルートを制限するにはどうすればよいですか?app.MyRouter.navigate('/my_resource/some_thing')notFound

4

1 に答える 1

2

細かいマニュアルから:

ルート router.route(route, name, [callback])

ルーターのルートを手動で作成します。route引数は、ルーティング文字列または正規表現です。ルートまたは正規表現からの一致する各キャプチャは、引数としてコールバックに渡されます。

したがって、いつでも次のように言うことができます。

this.route(/my_resource\/(\d+)/, 'show')

initializeBackbone の文字列パターンよりも細かいルート制御が必要な場合は、ルーターで使用します。

于 2013-09-24T20:27:04.463 に答える