【问题标题】:backbone js, routing error and url is auto change主干js,路由错误和url是自动更改的
【发布时间】:2013-06-15 20:05:37
【问题描述】:

我的路由主干有问题。我在产品索引 (localhost:3000/products) 中调用 Backbone.history.start()。路线:

'': 'product_index'  
':id': 'product_show' (tag 'a' link 'localhost:3000/products#123')
':id/items/:item_id': 'item_show' (tag 'a' link 'localhost:3000/products#123/items/456')

注意:所有标签都有 data-ajax = 'false' 并且有一些配置:

$(document).bind('mobileinit', function() {
        $.mobile.ajaxEnabled = false;
        $.mobile.linkBindingEnabled = false;
        $.mobile.hashListeningEnabled = false;
        $.mobile.pushStateEnabled = false;
      });

当我在页面 'localhost/products#123' 并单击标签 'a' 链接 'localhost:3000/products#123/items/456' 时发生错误。它警告“加载页面错误”,然后 url 自动更改为“localhost:3000/123/items/456”,并且在控制台中出现错误:“GET localhost:3000/123/items/456 404 (Not Found)”

我的路线文件:

class Braindu.Routers.Mobile extends Backbone.Router
  initialize: (options) ->
    @products = new Braindu.Collections.Products()
    @products_view = new Braindu.Views.MobileProductsIndex(collection: @products, id_product_el:'product-index-page')

  routes:
    ''    : 'product_index'
    ':id' : 'product_show'
    ':id/items/:item_id': 'item_show'


  product_index: ->
    @product_index_view.render()

  product_show:(id) ->
    $.mobile.changePage( "#product-show-page" , { reverse: false, changeHash: false } )
    current_product_model = @product_index_view.product_collection.where({_id: id})[0]
    if current_product_model != null && current_product_model != undefined
      @product_index_view.render_current_product(current_product_model)

  item_show:(id, item_id)->
    console.log 'item showwwwwwwwwwwwwwwwwwwwwwww'
    $.mobile.changePage( "#object-card-page" , { reverse: false, changeHash: false } )

【问题讨论】:

  • 请分享您的路由代码以及您在哪里进行 GET 调用。
  • 我添加了 func 路由。请看

标签: jquery-mobile backbone.js backbone-routing


【解决方案1】:

当您调用 localhost:3000/123/items/456 时,您是在调用以下函数吗?

 product_show:(id) ->
    $.mobile.changePage( "#product-show-page" , { reverse: false, changeHash: false } )
    current_product_model = @product_index_view.product_collection.where({_id: id})[0]
    if current_product_model != null && current_product_model != undefined
      @product_index_view.render_current_product(current_product_model)

如果是这样,请为我调试并找出 id 的值。

谢谢

【讨论】:

  • 当我在页面'localhost/products#123'并单击带有href'localhost:3000/products#123/items/456'的标签'a'时发生错误。它不去 item_show 功能。 product_show 函数工作正常,id 值为:123
  • 尝试按照这个答案,但仍然得到错误。 stackoverflow.com/questions/10870948/…
  • 您是否使用导航属性进行导航? appFsMvc.App.navigate("create", { trigger: true });
  • 我不知道使用什么导航。我只是调用 Backbone.history.start()。我使用 gem 'backbone-on-rails'
  • 我添加路由:'test'(localhost:3000/products#test) 运行正常。但是 'test/abc' (localhost:3000/products#test/abc) 是上面的错误。我是否遗漏了一些用于骨干网或移动设备的配置??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
相关资源
最近更新 更多