Backbone JS如何渲染特定页面

发布于 2024-12-17 06:12:21 字数 831 浏览 0 评论 0原文

我刚刚开始使用 Backbone JS,因为我们正在启动一个 JS 密集型应用程序,我想遵循一个约定,以防止该项目在接下来的几个月和几年里看起来很糟糕。

这可能是一个简单的问题,但我无法弄清楚。

我有正常的 RESTful 操作/视图等,并添加了另一个通话信息只是为了玩。单击相应链接时,这将正确呈现索引页面。

:javascript
  $(document).ready(function() {
  $("#client-clicker").click( function() {
    window.router = new Pm.Routers.ClientsRouter({clients: #{@clients.to_json.html_safe}});
    Backbone.history.start();
  });     
});

现在的问题是:

它如何知道要转到 ClientsRou​​ter 的索引操作?

如何指定编辑、显示或信息操作?

提前致谢!

我从未使用过 js 的“约定”,但骨干 js 很有意义!

更新

我确实发现,如果我删除客户端路由器中的这一行,默认索引就会中断。

  routes:
"/new"      : "newClient"
"/index"    : "index"
"/:id/edit" : "edit"
"/:id"      : "show"
".*"        : "index" <----- #deleted and it broke
"/info"     : "info"

我仍然不确定如何点击特定视图。

I'm just starting to play with Backbone JS as we are starting a very JS intensive application and I want to follow a convention to keep the project from looking horrific in later months and years.

This is probably a simple question but I can't figure it out.

I have the normal RESTful actions/views etc. and have added another call info just to play with. This renders the index page correctly when the respective link is clicked.

:javascript
  $(document).ready(function() {
  $("#client-clicker").click( function() {
    window.router = new Pm.Routers.ClientsRouter({clients: #{@clients.to_json.html_safe}});
    Backbone.history.start();
  });     
});

Now for the questions:

How does this know to go to the index action of the ClientsRouter?

How do I specify the edit, show or info actions?

Thanks in advance!

I have never used a "convention" for js but backbone js just makes sense!

Update

I did find that if I delete this line in the Clients Router, the default to index breaks.

  routes:
"/new"      : "newClient"
"/index"    : "index"
"/:id/edit" : "edit"
"/:id"      : "show"
".*"        : "index" <----- #deleted and it broke
"/info"     : "info"

I'm still not sure how to hit a specific view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜血染红嫁衣 2024-12-24 06:12:21

当您调用 Backbone.history.start 时,Backbone 开始监视位置的更改,无论是使用 Pushstate 或 hashchange 事件,还是在旧版浏览器中轮询位置。它还通过将 window.location.pathname 与路由表进行比较,立即触发当前位置的路由。

在您的情况下,它与“.*”路线匹配。我不确定发生这种情况时的位置是什么,但这条路线将匹配任何位置,这就是它触发索引操作的原因。

您可以通过调用路由器上的 navigate 方法来更改位置并选择性地触发操作。例如

window.router.navigate('/new', true);

应该更改位置并(因为您在第二个参数中传递 true)触发 newClient 操作。

When you call Backbone.history.start Backbone starts watching for changes to the location, either using pushstate or hashchange events or in older browsers polling the location. It also immediately triggers the route for the current location by comparing window.location.pathname with the route table.

In your case it matches on the ".*" route. I am not sure what the location is at the time this happens but this route will match any location so that's why it is triggering the index action.

You can change location and optionally trigger an action by calling the navigate method on your router. e.g.

window.router.navigate('/new', true);

should change the location and (because you are passing true in the second parameter) trigger the newClient action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文