简体   繁体   English

如何使Backbone.js路由器在CodeIgniter中的默认/索引(无#tag hashtag)上触发

[英]How to get Backbone.js Router to trigger on default/index (no #tag hashtag) in CodeIgniter

I have a CodeIgniter app using .htaccess to remove index.php from the url so they look like this: 我有一个使用.htaccess的CodeIgniter应用程序,从网址中删除index.php,因此它们看起来像这样:

http://example.com/admin/
http://example.com/admin/users
http://example.com/admin/reports
http://example.com/admin/files

With http://example.com/admin/files being a single page backbone app, I need to show a list of files by default. 由于http://example.com/admin/files是单页主干应用程序,因此默认情况下,我需要显示文件列表。 Then http://example.com/admin/files#file/123 would edit a specific file. 然后http://example.com/admin/files#file/123将编辑特定文件。

Here is my Backbone Router: 这是我的骨干路由器:

App.Router = Backbone.Router.extend({
    routes: {
      ''         : 'files',
      'file/:id' : 'file'
    },

    files: function(){
      $(document.body).append("Files route has been called..");
    },

    file: function(id){
      $(document.body).append("File route called with id:" + id);
    }

});
new App.Router;
Backbone.history.start();

I can get the "file" route to trigger with: 我可以通过以下方式获取“文件”路由来触发:

http://example.com/admin/files#file/33

However, http://example.com/admin/files is completely ignored by the Backbone Router. 但是,骨干路由器完全忽略了http://example.com/admin/files

How can I get the default or index Backbone.js route to trigger when using CodeIgniters RewriteRule that eliminates the index.php file? 使用CodeIgniters RewriteRule消除index.php文件时,如何获取默认或索引Backbone.js路由来触发?

UPDATE: I'm very new at Backbone.js so still learning. 更新:我对Backbone.js非常陌生,因此仍在学习中。 I discovered if I change: 我发现是否可以更改:

Backbone.history.start();

to: 至:

Backbone.history.start({root: "/admin/files/" });

It works correctly. 它可以正常工作。 :) :)

The Backbone Router works with the browsers history api where it uses hash names (#something) to represent some state on the client. 骨干路由器与浏览器历史记录api一起使用,在其中使用哈希名称(#something)表示客户端上的某些状态。 Initially, /admin/files is a route on the server and ignored on the client so you won't be able to use the Router for the files route. 最初,/ admin / files是服务器上的路由,在客户端上被忽略,因此您将无法使用路由器进行文件路由。 Since your page is rendered for the server route, I would just run whatever code you need to run on page load. 由于您的页面是针对服务器路由呈现的,因此我将只运行需要在页面加载时运行的任何代码。 I think that is what you want. 我想这就是你想要的。 If you need someway to get back to this initial "files" state, then you probably add an index route so that it would show up like /admin/files#index which would get you back to when the page first initially loaded. 如果您需要某种方式返回到此初始“文件”状态,则可以添加一个索引路由,以便它像/ admin / files#index一样显示,这将使您回到首次首次加载页面时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM