Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将查询字符串传递到backbone.js路由_Backbone.js_Query String_Url Routing - Fatal编程技术网

如何将查询字符串传递到backbone.js路由

如何将查询字符串传递到backbone.js路由,backbone.js,query-string,url-routing,Backbone.js,Query String,Url Routing,我正在使用Backbone.js和jQuery mobile。jQuery移动路由已禁用,我仅将lib用于UI。除了选择页面转换,我让一切都正常工作。我需要将页面转换(‘向上切片’、‘淡入’、‘向下滑动’)传递到主干路由器,因为转换根据用户来自何处而有所不同 我想出了一个非常丑陋的方法,通过url传递它们: class App.Routers.Foods extends Backbone.Router routes: '': 'index' ':transition': 'i

我正在使用Backbone.js和jQuery mobile。jQuery移动路由已禁用,我仅将lib用于UI。除了选择页面转换,我让一切都正常工作。我需要将页面转换(‘向上切片’、‘淡入’、‘向下滑动’)传递到主干路由器,因为转换根据用户来自何处而有所不同

我想出了一个非常丑陋的方法,通过url传递它们:

class App.Routers.Foods extends Backbone.Router
  routes:
    '': 'index'
    ':transition': 'index'
    'foods/new': 'new'
    'foods/new/:transition': 'new'

  initialize: ->
    @collection = new App.Collections.Foods()
    @collection.fetch()

  index: (transition)->
    view = new App.Views.FoodIndex(collection: @collection)
    App.changePage(view, transition)

  new: (transition)->
    view = new App.Views.FoodNew(collection: @collection)
    App.changePage(view, transition)
以下是默认转换的html链接:

<a href="#" data-icon="back" class="ui-btn-left">Back</a>
<a href="#fade" data-icon="back" class="ui-btn-left">Back</a>

以下是淡入淡出过渡的html链接:

<a href="#" data-icon="back" class="ui-btn-left">Back</a>
<a href="#fade" data-icon="back" class="ui-btn-left">Back</a>

使用查询字符串,即/food/new?transition='fade'肯定更好,但我不知道如何定义主干路由以使用查询字符串变量

我该怎么做


是否有一种更优雅的方法来处理我的问题,即根本不使用url传递变量?

您必须在路由器函数中手动解析url参数

class App.Routers.Foods extends Backbone.Router
  routes:
    '': 'index'
    'foods/new': 'new'

  initialize: ->
    @collection = new App.Collections.Foods()
    @collection.fetch()

  index: ()->
    view = new App.Views.FoodIndex(collection: @collection)
    App.changePage(view, getQueryVariable('transition'))

  new: ()->
    view = new App.Views.FoodNew(collection: @collection)
    App.changePage(view, getQueryVariable('transition'))
解析查询参数

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return unescape(pair[1]);
        }
    }
    return false;
}
函数getQueryVariable(变量){ var query=window.location.search.substring(1); var vars=query.split(&); 对于(变量i=0;i
当然,您必须将JS函数转换为CS,但您明白了。

unescape
2012年?我建议将
escape
unescape
放在历史的垃圾箱中,
encodeURIComponent
通常是您想要使用的。这是一个问题。路由与包含查询字符串的url不匹配。正则表达式应该用来定义允许查询字符串的路由。我发现这个主干插件正是我需要的!