在Backbone.js的控制器默认路由? [英] Default Routes in a Backbone.js controller?

查看:120
本文介绍了在Backbone.js的控制器默认路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为我的Backbone.js的控制器默认路由。目前,我做它像这样:

I want to set a default route for my backbone.js controller. Currently I do it like so:

class DealSearchController extends Backbone.Controller
    routes:
        'list' : 'showListView'
        'photos' : 'showPhotoView'
        'map' : 'showMapView'

    initialize: ->
        ....        
            window.location.hash = 'list' if ! _.include( _.keys(@routes),(window.location.hash || '').replace('#',''))

是否有这样做的更好的办法?

Is there a better way of doing it?

推荐答案

尝试添加该附加路由作为控制器的最后路线:

Try adding this additional route as the last route in your controller:

'*path':  'defaultRoute'

和再处理它是这样的:

defaultRoute: function(path) {
    this.showListView();
}

这个假设名单路线是你的preferred默认。这应该工作,因为Backbone.js的匹配,以便路线,但始终匹配'图示'的路线。

This assumes the list route is your preferred default. This should work since Backbone.js will match the routes in order, but will always match the 'splat' route.

这篇关于在Backbone.js的控制器默认路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆