Backbone.js的:如何从一个视图到另一个视图重定向? [英] Backbone.js: how to redirect from one view to another view?

查看:113
本文介绍了Backbone.js的:如何从一个视图到另一个视图重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Backbone.js的,什么是Htt的presponseRedirect的在Django相当于?

In Backbone.js, what is the equivalent of HttpResponseRedirect in Django?

在一个Backbone.View,我需要将数据提交到服务器,然后将用户重定向到另一个视图。这个怎么用?我应该使用的事件或路由器?

In one Backbone.View, I need to submit data to the server, and then redirect the user to another view. How does this work? Should I use event or router?

谢谢!

推荐答案

要了解这一点很重要的是,<一个href=\"http://lostechies.com/derickbailey/2011/08/03/stop-using-backbone-as-if-it-were-a-stateless-web-server/\">Backbone是不是无状态的Web服务器,所以重定向听起来像是你想要什么,但它最有可能不是。如果您在构建有状态的桌面应用程序的经验,从经验,以及如何将移动从一个屏幕到另一个画。如果你没有任何的桌面客户端的经验,我强烈建议你得到一些你的腰带,因为骨干应用程序是有状态的应用程序,它只是碰巧在浏览器中运行。

It's important to understand that Backbone is not a stateless web server, so a "redirect" might sound like what you want, but it most likely isn't. If you have any experience in building stateful desktop applications, draw from that experience and how you would move from one screen to another. If you don't have any desktop client experience, I highly recommend you get some of that under your belt because a Backbone app is a stateful app, it just happens to run in a browser.

假设你已经有了一些应用程序状态的知识,你有两种选择使这项工作。

Assuming you've got some stateful app knowledge, you have a couple of options for making this work.

您的服务器呼叫回来后,你可以打电话给你的路由器的导航方法: myRouter.navigate(/ myRoute,真)。一定要通过真正参数作为第二个参数。或者,像JasonOffutt建议,只是手动更新URL的杂凑片段,它有效的是同样的事情。

After your call to the server has come back, you can call your router's navigate method: myRouter.navigate("/myRoute", true). Be sure to pass the true argument as the second parameter. Or, like JasonOffutt suggest, just manually update the url's hash fragment and it's effectively the same thing.

<一个href=\"http://lostechies.com/derickbailey/2011/08/28/dont-execute-a-backbone-js-route-handler-from-your-$c$c/\">I'm不喜欢这个的,但有些人。

您可以创建一个非常简单的对象,它知道如何把右视图在屏幕上,当你调用一个方法。例如,如果你需要显示一个名为WidgetView视图中创建了一个名为方法 showWidgetView 的对象。通过它连接到一个命名空间,它传递给你的跑步code,或其他一些技术使在你的code随处可用此方法的对象。当你的服务器返回调用,调用该对象的 showWidgetView 方法,让它显示您的下一个视图。

You can create a very simple object that knows how to put the right view on the screen, when you call a method. For example, if you need to show a view called "WidgetView", create an object with a method called showWidgetView. Make the object with this method available anywhere in your code by attaching it to a namespace, passing it to your running code, or some other technique. When your call to the server returns, call the object's showWidgetView method and let it show the next view for you.

这是功能性的,但它比真正的小应用程序之外的任何迅速变得混乱。我做这个小应用程序,虽然。当你到larget的应用程序,你需要考虑工作流事件触发和事件驱动的架构。

This is functional, but it gets messy quickly in anything other than really small apps. I do this in small apps, though. When you get to larget apps, you need to think about evented workflow and event-driven architectures.

在你的服务器调用返回,断火使用类似<一个事件href=\"http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/\">an事件聚合,让你的系统的其他部分知道一些重要的事情就这样发生了。那么系统的这些其他部分可以找出做什么回应,包括呼叫code,它用一个新的替换当前视图,更新URL路径,等等。

After your server call returns, fire off an event using something like an event aggregator, letting other parts of your system know that something important just happened. These other parts of the system can then figure out what to do in response, including call code that replaces the current view with a new one, updates the url route, etc.

当你去使用事件的路径,最终你会碰到code簇的似乎并不适合在任何地方,很好。这通常是事件处理程序,并通常是一个对象/关切的是要被封装的标志。在骨干创建更高层次的工作流对象是一个好主意,如果你使用的是事件驱动的架构。

When you go down the path of using events, eventually you'll run into a cluster of code that doesn't seem to fit anywhere, nicely. This is usually your event handlers, and is usually a sign of an object / concern that wants to be encapsulated. Creating higher level workflow objects in Backbone is a good idea if you're using an event driven architecture.

这些工作流对象通常是通过让所有到位的右视图和模型踢了一个工作流负责,然后包含事件处理程序的意见和/或事件聚合器。该事件处理方法会检查应用程序的状态,无论是通过通过该方法通过指定参数时,或者看一些其他国家,并找出哪些需要接下来会发生什么。然后,它会导致应用程序更改到任何需要的样子,根据新的状态。

These workflow objects are generally responsible for kicking off a workflow by getting all of the right views and models in place, and then contain event handlers for your views and / or event aggregator. The event handler methods will check the state of the app either through the args passed through the method, or by looking at some other state, and figure out what needs to happen next. Then it will cause the app to change to whatever it needs to look like, based on the new state.

此外,工作流对象是<一个href=\"http://lostechies.com/derickbailey/2011/08/30/dont-limit-your-backbone-apps-to-backbone-constructs/\">not骨干构建。这件事情,你会建造你自己,只是用普通的旧的JavaScript。

Also, a workflow object is not a backbone construct. It's something that you'll build yourself, just using plain old JavaScript.

...

希望有所帮助。

这篇关于Backbone.js的:如何从一个视图到另一个视图重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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