Backbone.js route optional parameter

前端 未结 4 386
生来不讨喜
生来不讨喜 2021-02-01 01:51

Is it possible to have optional parameters in a Backbone.js route?

e.g this:

routes:
  \"search/[:query]\": \"searchIndex\"

instead of:

4条回答
  •  孤街浪徒
    2021-02-01 02:36

    As of Backbone 0.9.9, you can add optional paramaters with parentheses.

    For example in your routes object you can define an optional route part like this:

    routes: {
        "organize(/:action)": "displayOrganize"
    }
    

    Now the url path will match /#organize and routes like /#organize/create.

    Keep in mind that if you need routes like /#organize/ (with a trailing slash) to be recognized, you can do:

    routes: {
        "organize(/)(:action)": "displayOrganize"
    }
    

提交回复
热议问题