Backbone.js route optional parameter

前端 未结 4 422
生来不讨喜
生来不讨喜 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:45

    Probably the most easiest way is just declare more than one route, one with the extra arg,one without:

    routes:{
            "authProxy/:hash": "authProxy",                                                                                                                                                                 
            "authProxy/:hash/:url": "authProxy"
    }
    

    then just check for them in your method:

    authProxy: function(hash, url){
        if (url){
          // Hash and URL.
        }else{
          // Just hash.
        }
    }
    

    Note that I like this much better than the other two answers because it's very easy for another developer to understand what's going on.

提交回复
热议问题