2.3 The Object Model -- Computed Properties
一、What are computed properties?
1. 简而言之,计算属性让你声明函数为属性。你通过定义一个计算属性作为一个函数来创建一个,当你请求这个属性时,Ember会自动调用这个function。
之后你可以用同样的方法使用它,任何正常静态属性。
2. 对于获取一个或多个正常的属性和转换或者操纵它们的数据去创建一个新的值,它是超级方便的。
二、Computed Properties In Action
example:
Person = Ember.Object.extend({
//these will be supplied by 'create'
firstname: null,
lastName: null fullName: Ember.computed('firstName', 'lastName', function () {
return this.get('firstName') + ' ' + this.get('lastName');
});
}); var ironMan = Person.create({
firstName: "Tony",
lastName: "Stark"
}); ironMan.get('fullName');//"Tony Stark"
- 这里声明了一个function去作为一个计算的属性,参数告诉Ember它依赖于firstName和lastName属性。
- 不管什么时候你访问fullname属性,这个function会被调用,并返firstname+lastname的值。
三、Chaining Computed Properties(计算属性链)
可以使用计算属性作为值去创建新的计算属性。
example:这里为上一个例子添加description计算属性,使用存在的fullName属性并添加一些其他的属性。
Person = Ember.Object.extend({
firstName: null,
lastName: null,
age: null,
country: null, fullName: Ember.computed('firstName', 'lastName', function () {
return this.get('firstName') + ' ' + this.get('lastName');
}); description: Ember.computed('fullName', 'age', 'country', function () {
return this.get('fullName') + '; Age: ' + this.get('age') + '; Country:' + this.get('country');
});
}); var captainAmerica = Person.create({
firstName: 'Steve',
lastName: 'Rogers',
coutnry: 'USA''
}); captainAmerica.get('description');//"Steve Rogers; Age: 80; Country: USA"
四、Dynamic Updating
可计算的属性,默认情况下,观察它们所依赖的属性,并在调用时动态更新。让我们使用计算的属性来动态更新。
captainAmerica.set('firstName', 'William');
captainAmerica.get('description');//"William Rogers; Age:80; Country: USA"
- 这个firstName的改变被计算的属性fullName观察到,它自己被description属性观察到。
- 设置任何相关的属性都会通过任何依赖于它们的计算属性来传播变化,你已经创建了一条计算属性链。
五、Setting Computed Properties
当你设置计算的属性的时候你也可以定义Ember应该做什么。如果你试着设置计算的属性,它将会通过属性名被调用,你想设置的值和以前的值。
Person = Ember.Object.extend({
firstName: null,
lastName: null, fullName: Ember.computed('firstName', 'lastName', {
get(key) {
return this.get('firstName') + ' ' + this.get('lastName');
},
set(key, value) {
var [ firstName, lastName ] = value.split(/\s+/);
this.set('firstName', firstName);
this.set('lastName', lastName);
}
});
}); var captainAmerica = Person.create();
captainAmerica.set('fullName', "William Burnside");
captainAmerica.get('firstName');//William
captainAmerica.get('lastName');//Burnside
Ember将会为setters和getters调用计算的属性,如果你想要使用计算的属性来作为一个setter,你需要去检查参数的个数来确定是否是被作为一个getter和setter调用。如果一个值是从setter返回的,它将会被缓存在属性的值里。
2.3 The Object Model -- Computed Properties的更多相关文章
- 2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)
1. 通常,你可能有一个计算的属性依赖于数组中的所有元素来确定它的值.例如,你可能想要计算controller中所有todo items的数量,以此来确定完成了多少任务. export default ...
- 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?
有时候新用户在使用计算属性.绑定和监视者时感到困惑.下面是一些指导方针: 1. 使用computed properties来合成其他属性,以构建新的属性.computed properties不应该包 ...
- (3)选择元素——(2)文档对象模型(The Document Object Model)
One of the most powerful aspects of jQuery is its ability to make selecting elements in the DOM easy ...
- object model 概述
Object Model 综述 标准 C++ 的对象模型为对象的动态特性提供了运行时的支持. 但是它静态的本性决定了在某些领域它表现出僵化.不可扩展的特点. GUI编程就是一个既需要运行时编译的效率, ...
- Stored Properties 与 Computed Properties
Stored Properties 与 Computed Properties About Swift Stored Properties In its simplest form, a stored ...
- POM(project Object Model) Maven包管理依赖 pom.xml文件
什么是POM POM全称为“Project Object Model”,意思是工程对象模型.Maven工程使用pom.xml来指定工程配置信息,和其他文本信息.该配置文件以xml为格式,使用xml语法 ...
- [转]The Regular Expression Object Model
本文转自:https://docs.microsoft.com/en-us/dotnet/standard/base-types/the-regular-expression-object-model ...
- Component Object Model (COM) 是什么?
本文主要介绍 COM 的基础知识,倾向于理论性的理解,面向初学者,浅尝辄止. 1. COM 是什么: COM 的英文全称是,Component Object Model,中文译为,组件对象模型.它官方 ...
- Selenium的PO模式(Page Object Model)[python版]
Page Object Model 简称POM 普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...
随机推荐
- Strut2------获取界面返回的session,application,parameter
1.Action类下的代码 public class ServletActionDemo extends ActionSupport { @Override public String execute ...
- Mac下,如何把项目托管到Github上(Github Desktop的使用)
在上一篇中,详细讲解了使用X-code和终端配合上传代码的方法,这种方法比较传统,中间会有坑,英文看起来也费劲,不过Github官方提供了一个Mac版的客户端,如下图:
- vue2.0+element-ui(01简单点的单页面)
前言: 在<Vue.js权威指南>刚出版的时候,自己就作为一名前端粉捧了一把场,可是真是应了那句“出来混,总是要还的“这句话了,那时候信心满满的买来书想要认真研究,最终却还是把它搁浅了.直 ...
- 使用Jquery做分页效果
之前写过一个PHP 的分页效果,但是今天小伙伴和我说了一个不适用后台单纯用前段的JS来写分页,整理了一下,代码如下: html: <div id="containet"> ...
- jQuery中的$.each的用法
$.each(Array,function(i,value){ this; //this指向当前对象 i; //i表示当前下标 value; //value表示当前元素 })
- [020]Sencha Ext JS 6.0使用教程2
本节主要以典型例子介绍如何用Sencha Ext JS6.0进行项目开发 入门阶段总是比较难的,掌握了基本操作步骤,使用方法,架构思维,开发起来还是满顺利,开心的,自己又能掌握一门新技术,又能进步,主 ...
- Splay模板 1.0
struct Splay{ int rt,sz; ///根节点,树节点总数 ],fa[N];///值,左右儿子,父亲 void spin(int t){ ///旋转操作 ]==t; son[x][y] ...
- 微信小程序5.2.2版本,找不着resource下exml皮肤
问题描述: egret engine 5.2.2 原来5.1.11好好的,一升级就跪了 新建一个项目,找不到皮肤... 已发到论坛问去了,现在只能手动复制皮肤到小游戏目录下... 解决方案: 卸载重新 ...
- {sharepoint} SetPermission
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- [MongoDB] 安装MongoDB配置Replica Set
MongoDB的环境主要包括StandAlone,Replication和Sharding. StandAlone:单机环境,一般开发测试的时候用. Replication:主从结构,一个Primar ...