4.7 Routing -- Redirecting
一、Transitioning and Redirection
从一个route调用transitionTo或者从一个controller调用transitionToRoute将会停止任何进程中的任何跳转并且开启一个新的,作为重定向功能。
transitionTo携带的参数和行为和link-to辅助器完全一样:
- 如果你跳转进一个没有动态字段的路由,该路由的model hook任然会运行。
- 如果新的路由有动态字段,你需要为每一个字段传递一个model或者一个identifier (标识符)。传递一个model将会跳过字段的model hook。传递一个identifier将会运行model hook,并且你可以在参数中获取这个identifier (标识符)。
二、Before the model is known
如果你想要从一个路由重定向到另一个,你可以在路由的处理器的beforeModel hook做转换。
app/router.js
Router.map(function() {
this.route('posts');
});
app/routes/index.js
export default Ember.Route.extend({
beforeModel() {
this.transitionTo('posts');
}
});
三、After the model is known
如果你需要关于当前model的信息用来决定重定向,你应该使用afterModel或者redirect hook。它们接收解析过的model作为第一个参数并且transiton作为第二参数,因此function作为别名。(实际上,afterModel默认的实现就是调用redirect)
app/router.js
Router.map(function() {
this.route('posts');
this.route('post', { path: '/post/:post_id' });
});
app/routes/post.js
export default Ember.Route.extend({
afterModel(posts, transition) {
if (posts.get('length') === 1) {
this.transitionTo('post', posts.get('firstObject'));
}
}
});
当过跳转posts路由时如果它只有一条post数据,当前的跳转将终止,为了支持重定向到PostRoute,用一个单一的post object作为它的model。
四、Based on other application state(基于其他应用程序的状态)
1. 你可以基于一些其他应用程序的状态有条件的跳转:
app/router.js
Router.map(function() {
this.route('topCharts', function() {
this.route('choose', { path: '/' });
this.route('albums');
this.route('songs');
this.route('artists');
this.route('playlists');
});
});
app/routes/top-charts/choose.js
export default Ember.Route.extend({
beforeModel() {
var lastFilter = this.controllerFor('application').get('lastFilter');
this.transitionTo('topCharts.' + (lastFilter || 'songs'));
}
});
app/routes/filter.js
// Superclass to be used by all of the filter routes: albums, songs, artists, playlists
export default Ember.Route.extend({
activate() {
var controller = this.controllerFor('application');
controller.set('lastFilter', this.templateName);
}
});
- 在例子中,导航到/,URL立即跳转到用户所在的最后一个过滤URL。第一次,它跳转到/songs URL。
2. 你的路由也可以选择只在某些情况下跳转。如果beforeModel hook不终止或者跳转到一个新的路由,剩下的hooks(model, afterModel, setupController,renderTemplate)将会向往常一样执行。
4.7 Routing -- Redirecting的更多相关文章
- PatentTips - Transparent unification of virtual machines
BACKGROUND Virtualization technology enables a single host computer running a virtual machine monito ...
- ASP.NET路由[ASP.NET Routing]
ASP.NET路由[ASP.NET Routing] ASP.NET路由允许你在使用URL时不必匹配到网站中具体的文件,因为这个URL不必匹配到一个文件,你使用了描述用户行为且更容易被用户理解的URL ...
- 解读ASP.NET 5 & MVC6系列(12):基于Lamda表达式的强类型Routing实现
前面的深入理解Routing章节,我们讲到了在MVC中,除了使用默认的ASP.NET 5的路由注册方式,还可以使用基于Attribute的特性(Route和HttpXXX系列方法)来定义.本章,我们将 ...
- 解读ASP.NET 5 & MVC6系列(11):Routing路由
新版Routing功能介绍 在ASP.NET 5和MVC6中,Routing功能被全部重写了,虽然用法有些类似,但和之前的Routing原理完全不太一样了,该Routing框架不仅可以支持MVC和We ...
- [ASP.NET MVC 小牛之路]07 - URL Routing
我们知道在ASP.NET Web Forms中,一个URL请求往往对应一个aspx页面,一个aspx页面就是一个物理文件,它包含对请求的处理. 而在ASP.NET MVC中,一个URL请求是由对应的一 ...
- ASP.NET MVC Routing学习笔记(一)
Routing在ASP.NET MVC中是非常核心的技术,属于ASP.NET MVC几大核心技术之一,在使用Routing之前,得先引入System.Web.Routing,但其实不用这么麻烦,因为在 ...
- Routing 功能概述 - 每天5分钟玩转 OpenStack(98)
路由服务(Routing)提供跨 subnet 互联互通功能. 例如前面我们搭建了实验环境: cirros-vm1 172.16.100.3 vlan100 cirros-vm ...
- .NET/ASP.NET Routing路由(深入解析路由系统架构原理)
阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...
- 理解 OpenStack 高可用(HA)(3):Neutron 分布式虚拟路由(Neutron Distributed Virtual Routing)
本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...
随机推荐
- SQLServer------如何快速插入几万条测试数据
方法一: 1.建表 if OBJECT_ID('test') is not null drop table test go create table test (id ,),vid ), constr ...
- Emulator Error: Could not load OpenGLES emulation library: Could not load DLL!
Copy the file below from SDK\tools\lib to SDK\tools. libEGL_translator.dlllibGLES_CM_translator.dlll ...
- ionic ui框架及creator使用帮助
UI框架使用方法:http://ionicframework.com/docs/api/ PS:路由之类的其他js代码示例建议用 官方的app 生成器弄一个简单的页面,然后下载回来看 https:// ...
- LeetCode——Count Complete Tree Nodes
Description: Given a complete binary tree, count the number of nodes. In a complete binary tree ever ...
- deferred对象(摘自别人的文章)
对jQuery中的deferred对象的整体认识: Deferred是个工厂类,返回的是内部构建的deferred对象 tuples 创建三个$.Callbacks对象,分别表示成功,失败,处理中三种 ...
- Egret打包App Android热更新(4.1.0)
官网教程:http://developer.egret.com/cn/github/egret-docs/Native/native/hotUpdate/index.html 详细可看官网教程,我这里 ...
- android去除Spinner的分割线
<style name="TestSpinnerStyle" parent="android:style/Widget.ListView.DropDown" ...
- C#6.0语法特性
1.自动属性初始化的改进(有用) 原来的用法(声明时无法同时初始化),例如: class MyClass { public int Age { get; set; } public string Na ...
- 【转】SQL Server编程游标
在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: 而对于游标来说: ...
- EasyUI +MVC +EF实现增删改查
OA项目的框架已经搭建好了,接下来就是在这个框架下完成相应的业务的编码,接下来实现UserInfo页面的增删改查. 1.首先先谈一下遇到的一个框架上的问题:提示EF版本不一致之类的问题,主要是解决方案 ...