1.2 Getting Started--Naming Conventions(命名约定)
- app/routes/application.js
export default Ember.Route.extend({
model() {
return { title: "Hello World" };
}
});
- app/controllers/application.js
export default Ember.Controller.extend({
appName: 'My First Example'
});
- app/templates/application/hbs
<h1>{{appName}}</h1>
<h2>{{model.title}}</h2>
5. 在Ember.js应用程序中,你总会提供你的对象作为类,框架负责实例化它们并通过运行时解析器把它们提供给templates。
二、Simplement Routes(简单路由)
1. 每一个route都会有一个和它名字相同的controller和template。
2. A simple router:
var Router = Ember.Router.extend();
Router.map(function () {
this.route('favorites');
});
export default Router;
- 如果你的用户导航到/favorites,Ember.js将会在你的项目中需找下面这些类:
- app/routes/favorites.js
- app/controllers/favorites.js
- app/templates/favorites.js
- Ember.js将会把favorites template加载到application template的{{outlet}}中。它将会设置一个controller:favorites的实例作为template的controller。
- 如果你的app提供了一个route:favorites,这个框架会在加载template之前调用它。
3. 对于一个类似route:favorites这样的路由,你可能会实现model hook去指定你的controller将会把哪个model展现到模板。
example:
import ajax from 'ic-ajax';
export default Ember.Route.extend({
model() {
//the model is an Array of all of the posts
//fetched from this url
reutrn ajax('/a/service/url/where/posts/live');
}
});
- 在这个例子中,我们没有提供一个controller:favorites。因为model是一个数组,Ember.js会自动提供一个Ember.ArrayController的实例,它会展现backing Array作为它的模型。
- 你可以把Ember.ArrayController看做犹如模型本身。这么做的好处就是,你可以随时更换controller的model,而无需直接通知模板和组件的变化。
4. template可以遍历控制器的元素:
<ul>
{{#each controller as |item|}}
<li>{{item.title}}</li>
{{/each}}
</ul>
三、动态段(a URL that includes a parameter)
如果route使用动态段(一个包含参数的URL),路由的model将会基于用户提供的动态段的值。
1. 考虑一下这个路由定义
app/router.js
var Router = Ember.Router.extend();
Router.map(function () {
this.route('post', { path: '/posts/:post_id' });
});
export default Router
- 在这个例子中,route的名字是post,所以Ember.js将会查找这些对象:
- app/routes/post.js
- app/controllers/post.js
- app/templates/post.hbs
2. 路由的处理程序的model hook会把动态参数:post_id转换进model中。serialize hook把一个模型对象转换回这个路由的URL参数(例如,为模型对象生成一个link)。
app/routes/post.js
import ajax from 'ic-ajax'
export default Ember.Route.extend({
model(params) {
return ajax('/my-service/posts/' + params.post_id);
},
serialize(post) {
return { post_id: Ember.get(post, 'id') };
}
});
3. 因为这种现象非常普遍,它是默认的路由处理程序。
- 如果动态字段以_id结尾,默认的model hook将把第一部分转换为应用程序命名空间中的一个模型类(post looks for app/models/post.js)。然后它会调用类中的find获得动态字段的值。
- serialize hook的默认行为是用model object的id属性的值替换route的动态字段。
四、 Route,Controller和Template Defaults
1. 如果你没有为post route(app/routes/post.js)指定一个路由处理器,Ember.js仍然会使用app的app/controllers/post.js实例去渲染app/templates/post.hbs模板。
2. 如果你没有指定controller(app/controllers/post.js),Ember将会基于route的model hook返回的值生成一个。如果model是一个Array,你会得到一个ArrayController。或则,你会得到一个ObjectController。
3. 如果你没有指定一个post模板,Ember.js什么都不会渲染。
五、Nesting(嵌套)
你可以嵌套路由:
app/router.js
var Router = Ember.Router.extend();
Router.map(function () {
this.route('posts', function() { //the 'posts' route
this.route('favorites'); //the 'posts.favorites' route
this.route('post'); //the 'posts.post' route
});
});
export default Router
下面是这个路由器中每个route的命名约定:

六、The Index Route
在每个级别的嵌套(包括顶层),Ember.js自动为路径"/"提供一个名为index的route。
1. example:
app/router.js
var Router = Ember.Router.extend();
Router.map(function () {
this.route('favorites');
});
export default Router;
这相当于:
var Router = Ember.Router.extend();
Router.map(function () {
this.route('index', { path: '/' });
this.route('favorites');
});
export default Router;
如果用户访问'/',Ember.js将会查找这些对象:
- app/routes/index.js
- app/controllers/index.js
- app/templates/index.hbs
index模板将会渲染到application模板的{{outlet}}中。如果用户导航到/favorites,Ember.js将会使用favorites template替换index template。
2. 一个嵌套的路由器
app/router.js
var Router = Ember.Router.extend();
Router.map(function () {
this.route('posts', function () {
this.route('favorites');
});
});
export default Router;
这相当于:
app/router.js
var Router = Ember.Router.extend();
Router.map(function () {
this.route('index', { path: '/' });
this.route('posts', function () {
this.route('index', { path: '/' });
this.route('favorites');
});
});
export default Router;
如果用户导航到/posts,当前路由将会是posts.index,Ember.js将会查找以下objects:
- app/routes/posts/index.js
- app/routes/posts/index.js
- app/templates/posts/index.hbs
首先,posts模板将会被渲染到application模板的{{outlet}}中。然后,posts/index模板将会被渲染到posts模板的{{outlet}}中。
如果用户导航到posts/favorites,Ember.js将会用posts/favorites模板替换posts模板中的{{outlet}}。
1.2 Getting Started--Naming Conventions(命名约定)的更多相关文章
- Effective Java 56 Adhere to generally accepted naming conventions
Typographical naming conventions Identifier Type Type Examples Package com.google.inject, org.joda.t ...
- Naming Conventions for .NET / C# Projects
http://www.akadia.com/services/naming_conventions.html Naming Conventions for .NET / C# Projects Mar ...
- C# Coding & Naming Conventions
Reference document https://msdn.microsoft.com/en-us/library/ff926074.aspx https://msdn.microsoft.com ...
- Spring mvc 4系列教程(二)——依赖管理(Dependency Management)和命名规范(Naming Conventions)
依赖管理(Dependency Management)和命名规范(Naming Conventions) 依赖管理和依赖注入(dependency injection)是有区别的.为了将Spring的 ...
- JavaScript Patterns 2.10 Naming Conventions
1. Capitalizing Constructors var adam = new Person(); 2. Separating Words camel case - type the word ...
- Naming conventions of python
1.package name 全部小写字母,中间可以由点分隔开,作为命名空间,包名应该具有唯一性,推荐采用公司或组织域名的倒置,如com.apple.quicktime.v2 2.module nam ...
- Stackoverflow/dapper的Dapper-Extensions用法(一)
Dapper-Extensions Dapper Extensions is a small library that complements Dapper by adding basic CRUD ...
- Objective-C文章中的生词
Objective-C http://rypress.com/tutorials/objective-c/index C Basics http://rypress.com/tutorials/ ...
- ExtJS笔记2 Class System
For the first time in its history, Ext JS went through a huge refactoring from the ground up with th ...
随机推荐
- Codeforces Round #277.5 (Div. 2)部分题解
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 使用rlwrap调用sqlplus中历史命令
此文来自http://www.cnblogs.com/mchina/archive/2013/03/08/2934473.html 在此谢谢原创作者. 在Linux中运行SQL*Plus,不能调用历史 ...
- 新唐M0 M4系统初始化
系统初始化包含了时钟(clock)初始化和多功能引脚(Multi Function Pin 简称MFP寄存器)配置.void SYS_Init(void) { /* 解锁保护寄存器 */ SYS_Un ...
- vs删除空行 —— 正则表达式以及其他
\r:return 到当前行的最左边. \n: newline 向下移动一行,并不移动左右. 1. ^\s*\n 查找空行 2. ^\s*(?=\r?$)\n 查找空行即 ...
- AndroidのBuild工具之Ant动手实践
好久没有写博客了,没半年也应该有几个月了.在工作上的项目遇到过很多问题或者说积累了不少经验,曾经都蛮想发到博客留个纪念什么的,不求可以为别人获得点经验技巧,只求在多年后遇到同样的问题可以找到个记录.但 ...
- Linux学习——定义命令行函数(cd .. -> ..)
在使用shell的时候,每天要面对各种命令行,比如ls , cd .. 等 抱着简单,可依赖的思想.有些可以简化的操作可以要通过在 ~/.bashrc 中进行添加: 1. cd .. -> .. ...
- LeetCode——Best Time to Buy and Sell Stock III
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- jQuery的一些小技巧()
1.判断某元素上是否绑定了某种类型的事件 var $events = $("#foo").data("events"); if( $events &&a ...
- 【BZOJ3470】Freda’s Walk 概率与期望
[BZOJ3470]Freda’s Walk Description 雨后的Poetic Island空气格外清新,于是Freda和Rainbow出来散步. Poetic Island的交通可以看作一 ...
- jfinal关联查询给dto添加表结构以外的字段并返回的处理方式
官网栗子: http://www.jfinal.com/doc/5-10 5.10 表关联操作 JFinal ActiveRecord 天然支持表关联操作,并不需要学习新的东西,此为无招胜有招.表关联 ...