【前端】Ember.js学习笔记
Model
在默认情况下,model钩子返回的值,会设置为关联的控制器的model属性。例如,如果App.PostsRoute通过model钩子返回了一个对象,这个对象会设置为App.PostsController的model属性。
(模板是如何知道该使用哪个模型进行渲染的呢?模板通过查找其关联的控制器的model属性来进行渲染。例如,photos模板将会使用App.PhotosController的model属性来进行渲染。)
Ember Data
在Ember中,每个路由都有与之相关联的一个模型。这个模型可以通过路由的model钩子进行设置,可以通过给{{link-to}}传入一个参数,也可以通过调用路由的transitionTo()方法。
对于简单的应用来说,可以通过jQuery来从服务器加载JSON数据,并将这些JSON数据对象作为模型。
但是,使用一个模型库来管理查询、更改和将更改保存回服务器,将会大大的简化代码,同时也能提升应用的健壮性和性能。
许多Ember应用使用Ember Data来处理模型。Ember Data是一个与Ember.js紧密结合在一起的代码库,简化了客户端从服务器获取记录,在本地进行缓存以提高性能,保存修改到服务器,创建新的记录等一系列的操作。
Ember Data不需要进行任何配置,就可以实现通过服务端提供的RESTful JSON API加载和保存记录以及它们的管理关系,这些操作都遵从于特定的惯例。
目前,Ember Data还是一个独立于Ember.js的库。在Ember Data被作为标准发行版的一部分之前,你可以在builds.emberjs.com下载最新的版本。
仓库Store
仓库是应用存放记录的中心仓库。你可以认为仓库是应用的所有数据的缓存。应用的控制器和路由都可以访问这个共享的仓库;当它们需要显示或者修改一个记录时,首先就需要访问仓库。
DS.Store的实例会被自动创建,并且该实例被应用中所有的对象所共享。
模型
模型是一个类,它定义了需要呈现给用户的数据的属性和行为。任何用户期望在其离开应用然后再回到应用时能够看见的数据,都应该通过模型来表示。
例如,如果正在编写一个可以给饭店下单的Web应用,那么这个应用中应该包含Order、LineItem和MenuItem这样的模型。
模型定义了服务器提供的数据的类型。
模型也声明了它与其他对象的关系。
模型本身没有任何数据;模型只定义了其实例所具有的属性和行为,而这些实例被称为记录。
记录
记录是模型的实例,包含了从服务器端加载而来的数据。应用本身也可以创建新的记录,以及将新记录保存到服务器端。
记录由以下两个属性来唯一标识:
- 模型类型
- 一个全局唯一的ID
ID通常是在服务器端第一次创建记录的时候设定的,当然也可以在客户端生成ID。
适配器
适配器是一个了解特定的服务器后端的对象,主要负责将对记录的请求和变更转换为正确的向服务器端的请求调用。
例如,如果应用需要一个ID为1的person记录,那么Ember Data是如何加载这个对象的呢?是通过HTTP,还是Websocket?如果是通过HTTP,那么URL会是/person/1,还是/resources/people/1呢?
适配器负责处理所有类似的问题。无论何时,当应用需要从仓库中获取一个没有被缓存的记录时,应用就会访问适配器来获取这个记录。如果改变了一个记录并准备保存改变时,仓库会将记录传递给适配器,然后由适配器负责将数据发送给服务器端,并确认保存是否成功。
架构简介
应用第一次从仓库获取一个记录时,仓库会发现本地缓存并不存在一份被请求的记录的副本,这时会向适配器发请求。适配器将从持久层去获取记录;通常情况下,持久层都是一个HTTP服务,通过该服务可以获取到记录的一个JSON表示。

参考文献
杂项
Route: A route is an object that tells the template which model it should display.流程
No matter how the URL gets set, the first thing that happens is that the Ember
routermaps the URL to aroute handler.The
route handlerthen typically does two things:- It renders a template.
- It loads a model that is then available to the template.
Componentsconsist of two parts: a template written in Handlebars, and a source file written in JavaScript that defines the component's behavior.When we call
this.store.findAll('rental'), Ember Data will make a GET request to/rentals.In certain cases, you will want to pass arguments to _super() before or after overriding.
One common example is when overriding the normalizeResponse() hook in one of Ember-Data's serializers.
A handy shortcut for this is to use a "spread operator", like ...arguments:
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
// Customize my JSON payload for Ember-Data
return this._super(...arguments);
}
The above example returns the original arguments (after your customizations) back to the parent class, so it can continue with its normal operations.
If you are subclassing a framework class, like
Ember.Component, and you override theinit()method, make sure you callthis._super(...arguments)! If you don't, a parent class may not have an opportunity to do important setup work, and you'll see strange behavior in your application.Observers :
Observers are especially useful when you need to perform some behavior after a binding has finished synchronizing.
If you never get() a computed property, its observers will not fire even if its dependent keys change. You can think of the value changing from one unknown value to another.
It's important that we include some information in the URL about not only which template to display, but also which model. In Ember, this is accomplished by defining routes with
dynamic segments. 详情见:https://guides.emberjs.com/v2.5.0/routing/specifying-a-routes-model/#toc_dynamic-models
Objects in Ember
JavaScript objects don't support the observation of property value changes. Consequently, if an object is going to participate in Ember's binding system you may see an Ember.Object instead of a plain object.Ember.Object also provides a class system, supporting features like mixins and constructor methods.
Ember also extends the JavaScript Array prototype with its Ember.Enumerable interface to provide change observation for arrays.
Finally, Ember extends the String prototype with a few formatting and localization methods.
Classes and Instances
通过一段代码来理解:
Person = Ember.Object.extend({
say(thing) {
var name = this.get('name');
alert(`${name} says: ${thing}`);
}
});
Soldier = Person.extend({
say(thing) {
// this will call the method in the parent class (Person#say), appending
// the string ', sir!' to the variable `thing` passed in
this._super(`${thing}, sir!`);
}
});
var yehuda = Soldier.create({
name: 'Yehuda Katz'
});
yehuda.say('Yes'); // alerts "Yehuda Katz says: Yes, sir!"
Accessing Object Properties
When accessing the properties of an object, use the get() and set() accessor methods:
var person = Person.create();
var name = person.get('name');
person.set('name', 'Tobias Funke');
Make sure to use these accessor methods; otherwise, computed properties won't recalculate, observers won't fire, and templates won't update.
Reopening Classes and Instances
【前端】Ember.js学习笔记的更多相关文章
- ember.js学习笔记
启动服务器 ember server 访问localhost:4200 创建新的路由:ember generate route 路由名称,运行此命令会同时创建一个/templates/.XXXhbs模 ...
- JS 学习笔记--9---变量-作用域-内存相关
JS 中变量和其它语言中变量最大的区别就是,JS 是松散型语言,决定了它只是在某一个特定时间保存某一特定的值的一个名字而已.由于在定义变量的时候不需要显示规定必须保存某种类型的值,故变量的值以及保存的 ...
- 一点感悟:《Node.js学习笔记》star数突破1000+
写作背景 笔者前年开始撰写的<Node.js学习笔记> github star 数突破了1000,算是个里程碑吧. 从第一次提交(2016.11.03)到现在,1年半过去了.突然有些感慨, ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- Vue.js学习笔记(2)vue-router
vue中vue-router的使用:
- WebGL three.js学习笔记 使用粒子系统模拟时空隧道(虫洞)
WebGL three.js学习笔记 使用粒子系统模拟时空隧道 本例的运行结果如图: 时空隧道demo演示 Demo地址:https://nsytsqdtn.github.io/demo/sprite ...
- WebGL three.js学习笔记 法向量网格材质MeshNormalMaterial的介绍和创建360度全景天空盒的方法
WebGL学习----Three.js学习笔记(5) 点击查看demo演示 Demo地址:https://nsytsqdtn.github.io/demo/360/360 简单网格材质 MeshNor ...
- WebGL three.js学习笔记 创建three.js代码的基本框架
WebGL学习----Three.js学习笔记(1) webgl介绍 WebGL是一种3D绘图协议,它把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenGL ES 2.0的 ...
- vue.js 学习笔记3——TypeScript
目录 vue.js 学习笔记3--TypeScript 工具 基础类型 数组 元组 枚举 字面量 接口 类类型 类类型要素 函数 函数参数 this对象和类型 重载 迭代器 Symbol.iterat ...
随机推荐
- TortoiseGit 相关操作
1.TortoiseGit 记住用户名和密码的方法当你安装且配置好git后,在C:\Documents and Settings\Administrator\ 目录下有一个 .gitconfig 的 ...
- Qt容器类——1. QList类、QLinkedList类和QVector类
在开发一个较高性能需求的应用程序时,程序员会比较关注这些容器类的运行效率,表2.1列出了QList.QLinkedList和QVector容器的时间复杂度比较. 1.QList类 QList<T ...
- Windows Server 2008 R2父域管理员与子域管理员相互登录访问
一.问题: 父域创建的域管理员登录不了子域服务器,子域创建的域管理员登录不了父域服务器? 二.原因分析: 父域创建的域管理员是存在父域中,而子域创建的域管理员存在子域中,父域创建的管理员是不存在子域中 ...
- Mac iTerm2命令行快捷操作
control + R 搜索之前输入过的命令 control + U 删除整行命令 control + W 删除光标前面的命令 control + K 删除光标后面的命令
- 给php添加ssl证书
composer下载时报错: The "https://packagist.org/packages.json" file could not be downloaded: SSL ...
- 一个简单的配置管理器(SettingManager)
在很多.net开发的项目中,我们几乎都会使用到一些自定义的参数,比如说第三方的配置参数之类的. 他们的特点是:1.系统全局 2,可以做成键值对(Dictionary). 我们可以将这些参数放到Web. ...
- ios-深度解析二维码的生成与使用
利用一个小demo来对二维码进行学习,总共四个界面(主界面,生成二维码界面,识别二维码界面,扫描二维码界面) 一.二维码的介绍 1.什么是二维码? 二维条码/二维码是用某种特定的 ...
- JS/JQ获取各种屏幕的高度和宽度
Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- Extjs 一些配置以及方法
1.例如想要实现以下功能,本来model中只有用户的firstname和lastname,但是在grid中展示还需要展示用户姓名,或者只展示用户姓名