javascript - Getting URL fragment instead of hash in Backbone.js - Stack Overflow

I have struggled with this for quite a long time. I'm using Backbone.js along with Require.js. I&#

I have struggled with this for quite a long time. I'm using Backbone.js along with Require.js. I'm trying to tell Backbone to display a specific view according to the current URL (not hash). My code looks like this:

define(['jquery','underscore','backbone', 'views/manage_pages'], function($, _, Backbone,  ManagePages) {
  var AppRouter = Backbone.Router.extend({
    routes:{
      '/new_page': "showPageForm",
      '/edit_page': "showPageEditForm",
      '*actions': "showPageForm"
    },
  });
  var initialize = function(){
    appRouter = new AppRouter;
    appRouter.on("route:showPageForm", function(){
      console.log('hej');
    });
    appRouter.on("route:showPageEditForm", function(){
      console.log('ho');
    });
    var page_form = new ManagePages();
      Backbone.history.start();
    }
    return {
      initialize: initialize
    }
  });

So basically when the user goes to it should log <code>hej</code> and when he goes to it should log <code>ho</code>. I don't know how to acplish this or even if it is possible. Can anyone help me?

I have struggled with this for quite a long time. I'm using Backbone.js along with Require.js. I'm trying to tell Backbone to display a specific view according to the current URL (not hash). My code looks like this:

define(['jquery','underscore','backbone', 'views/manage_pages'], function($, _, Backbone,  ManagePages) {
  var AppRouter = Backbone.Router.extend({
    routes:{
      '/new_page': "showPageForm",
      '/edit_page': "showPageEditForm",
      '*actions': "showPageForm"
    },
  });
  var initialize = function(){
    appRouter = new AppRouter;
    appRouter.on("route:showPageForm", function(){
      console.log('hej');
    });
    appRouter.on("route:showPageEditForm", function(){
      console.log('ho');
    });
    var page_form = new ManagePages();
      Backbone.history.start();
    }
    return {
      initialize: initialize
    }
  });

So basically when the user goes to http://example./new_page it should log <code>hej</code> and when he goes to http://example./editpage it should log <code>ho</code>. I don't know how to acplish this or even if it is possible. Can anyone help me?

Share Improve this question edited Jun 22, 2017 at 16:08 laser 1,37613 silver badges14 bronze badges asked Nov 17, 2012 at 9:41 jbanjban 1491 silver badge10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You could use Backbone's support for HTML5 push-state, which uses the URL path by default (instead of the hash fragment). Just specify it as an option when calling history.start:

Backbone.history.start({ pushState: true });

(Note that if your application is located in a sub-path under the domain, then you can tell Backbone to use that as the root -- Backbone.history.start({pushState: true, root: "/myapproot/"}) -- although it looks like that's not a concern in your case.)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745257721a4619056.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信