创建具有Backbone.js的链接preferred方式 [英] Preferred way of creating links with backbone.js

查看:87
本文介绍了创建具有Backbone.js的链接preferred方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图环绕Backbone.js的我的头,但我发现很难,由于缺乏(IMO)很好的例子。

I'm trying to wrap my head around backbone.js but I'm finding it hard due to the lack of (IMO) good examples.

首先,是什么让一个链接对象的最佳方式。结果
如果我想获得一个专辑模式,我可以做的编辑网址 album.url()+'/编辑,这真的是最好的方式?

First of all, what is the best way of getting a link to an object.
If I want to get the edit url of an Album model I could do album.url() + '/edit', is this really the best way?

另外,我试图让我的应用程序的工作100%,无JavaScript的,所以我不希望我的网址/链接说 /专辑/#1 /编辑 ,我想这是 /专辑/ 1 /编辑并在JS覆盖。

Also, I'm trying to make my application work 100% without javascript so I don't want my URLs/links to say /albums/#1/edit, I want it to be /albums/1/edit and override this in JS.

我想我建立正常的网址,并使用 jQuery.live 中的骨干打电话 router.navigate 。 JS结果
我从来没有然而这个工作,当我打电话 router.navigate('/专辑/ 2',真)的URL的变化,但我的表演动作不会被调用。如果我刷新,它被称为所以路由匹配。

I'm thinking I create normal URLs and use jQuery.live to call router.navigate in backbone.js
I never got this to work however, when I call router.navigate('/albums/2', true) the URL changes but my show action is never called. If I refresh it's called so the route is matched.

我是什么失踪?

推荐答案

最基本的答案,这是一种令人沮丧的,是没有preferred办法!。 Backbone.js的不会告诉你如何设置链接,你可以做任何你喜欢的方式。我发现你做,至少在一开始这种灵活性就像讨厌​​的。

The basic answer, which is kind of frustrating, is "there is no preferred way!". Backbone.js doesn't tell you how to set up links, you can do it any way you like. I found this flexibility just as annoying as you do, at least at first.

因此​​,这里是我的我的当前项目,与(大)警告,这仅仅是一个许多的方式来做事的骨干:

So here's the way I'm approaching this on my current project, with the (big) caveat that this is just one of many ways to do things in Backbone:


  • 在大多数情况下,我不使用实际的链接。有没有明确的理由不,但它意味着你必须保持跟踪一大堆的必须是一致的URL字符串的。我宁愿坚持所有的URL在我的路由器格式与它在别处无法处理。

  • For the most part, I don't use actual links. There's no explicit reason not to, but it means you have to keep track of a bunch of URL strings that have to be consistent. I would rather stick all the URL formatting in my routers and not deal with it elsewhere.

要打开一个新的顶级的说法,像一个编辑屏幕,我设置的东西,触发一个事件。在应用我目前的工作,我有一个全球性的国家模式,并打开一个新的观点我称之为 state.set({冠捷:MyTopView})。这将导致状态对象触发的变化:冠捷

To open a new "top-level" view, like an editing screen, I set something that fires an event. In the application I'm currently working on, I have a global State model, and to open a new view I call state.set({ topview: MyTopView }). This causes the state object to trigger change:topview.

任何一块需要改变时,顶层视图更改已绑定更新方法UI的变化:冠捷。当事件触发时,他们在看 state.get('冠捷')并根据需要更新。

Any piece of the UI that needs to change when the top-level view changes has an update method bound to change:topview. When the event fires, they look at state.get('topview') and update as necessary.

我把我的路由器,因为只有用户界面的略专业化零部件 - 他们实际上是用于呈现在浏览器地址栏,而不是窗口的意见。像其他的看法,他们更新状态的UI事件的对象(即一个新的URL),而像其他的意见,他们听状态对象,导致他们更新的变化。 ALBUMID>该编辑屏幕拥有URL 专辑/&LT的逻辑/编辑被完全封装在路由器,我不指任何其他地方

I treat my routers as only marginally specialized parts of the UI - they're essentially views that render in the browser address bar, rather than the window. Like other views, they update the state object on UI events (i.e. a new URL), and like other views, they listen to the state object for changes that cause them to update. The logic that the editing screen has the URL albums/<albumid>/edit is fully encapsulated in the router, and I don't refer to it anywhere else.

这很适合我,但它增加了一个全新的格局,全球State对象,骨干结构,使我几乎不能称之为preferred的做法。

This works well for me, but it adds an entirely new pattern, the global State object, to the Backbone structure, so I can hardly call this the "preferred" approach.

更新:另外请注意,的.url(),在骨干成语,指的是模型的URL中的背景最终API 的,而不是前端URL(它不喜欢Django的 get_absolute_url )。有一个在默认设置骨干,让你的模型面向用户的URL没有一种方法 - 你不得不这样自己写

Update: Also note that .url(), in the Backbone idiom, refers to the model's URL in the back-end API, not the front-end URL (it's not like Django's get_absolute_url). There is no method in the default Backbone setup that gives you a user-facing URL for your model - you'd have to write this yourself.

这篇关于创建具有Backbone.js的链接preferred方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆