Modifying Attributes

1. 一旦一条record被加载,你可以开始改变它的属性。在Ember.js对象中属性的行为就像正常的属性。作出改变就像设置你想要改变的属性一样简单:

var tyrion = this.store.findRecord('person', 1);
// ...after the record has loaded
tyrion.set('firstName', "Yollo");

2. 对于修改属性来说,所有的Ember.js的方便性都是可用的。例如,你可以使用Ember.ObjectincrementProperty辅助器:

person.incrementProperty('age'); // Happy birthday!

3. 通过检查它的isDirty属性,你可以告诉一条发生显著改变的record是否被保存了。通过使用changedAttributes函数,你也可以发现record的哪一部分发生了变化并且原始值是什么。changedAttributes返回一个对象,它的keys是发生变化的属性并且值是一个values数组[oldValue, newValue]

person.get('isAdmin');      //=> false
person.get('isDirty'); //=> false
person.set('isAdmin', true);
person.get('isDirty'); //=> true
person.changedAttributes(); //=> { isAdmin: [false, true] }

4. 在这一点上,你可以通过save()持久化你的变化或者你可以回滚你的改变。调用rollback()还原所有changedAttributes到原始值。

person.get('isDirty');      //=> true
person.changedAttributes(); //=> { isAdmin: [false, true] } person.rollback(); person.get('isDirty'); //=> false
person.get('isAdmin'); //=> false
person.changedAttributes(); //=> {}

7.7 Models -- Working with Records的更多相关文章

  1. 7.6 Models -- Finding Records

    Ember Data的store为检索一个类型的records提供一个接口. 一.Retrieving a single record(检索单记录) 1. 通过type和ID使用store.findR ...

  2. 7.4 Models -- Pushing Records into the Store

    一.概述 1. store是作为一个所有records的缓存,这些records已经被你的应用程序加载.在你的app中如果你的路由或者一个controller请求一条record,如果它在缓存中这个s ...

  3. 7.5 Models -- Persisting Records

    一.概述 1. 在Ember Data上以每个实例为基础,records被持久化.在DS.Model的任何一个实例上调用save()并且它将产生一个网络请求. 2. 下面是一些例子: var post ...

  4. 7.3 Models -- Creating And Deleting Records

    一.Creating 1. 你可以通过调用在store中的createRecord方法来创建records. store.createRecord('post', { title: 'Rails is ...

  5. Models

    Models Models control the data source, they are used for collecting and issuing data, this could be ...

  6. 课程五(Sequence Models),第三周(Sequence models & Attention mechanism) —— 2.Programming assignments:Trigger word detection

    Expected OutputTrigger Word Detection Welcome to the final programming assignment of this specializa ...

  7. 7.9 Models -- Connection to An HTTP Server

    一.概述 1. 如果你的Ember应用程序需要从一个HTTP服务器加载JSON数据,在你的服务器返回的任何格式中,配置Ember Data的过程将会加载records. 2. store使用一个被称为 ...

  8. 7.8 Models -- The Rest Adapter

    一.概述 默认的,store将会使用 DS.RESTAdapter来加载和存储records.这个RESTAdapter假定URLS和JSON关联每一个model是约定好的:这意味着,如果你遵循这个规 ...

  9. 7.1 Models -- Introduction

    一.概述 1. 模型是表示应用程序呈现给用户的底层数据的对象.不同的应用程序有不同的模型,这取决于它们正在试图解决什么问题. 2. 例如,一个照片共享应用程序可能有一个Phone模型来代表一个特殊的照 ...

随机推荐

  1. flex常用兼容写法

    一般放在common.css中: .flex{ display: -webkit-box; display: -webkit-flex; display: -moz-box; display: -ms ...

  2. Ubuntu下Chrome运行Silverlight程序

    Ubuntu 14.04.1下运行Terminal,安装Pipelight输入以下命令: sudo add-apt-repository ppa:pipelight/stable sudo apt-g ...

  3. 教你如何将word中的表格完美粘贴到ppt中

    经常操作办公软件的人一定有一个困惑,ppt本身表格编辑能力似乎很弱,如果从word里直接将一个编辑好的表格复制粘贴到ppt中,整个表格一定会发生让你没有预料的变化,还得重新花老大的劲去重新编辑,有人屛 ...

  4. Maven:版本管理 【SNAPSHOT】【Release】【maven-release-plugin】【nexus】

    什么是版本管理 首先,这里说的版本管理(version management)不是指版本控制(version control),但是本文假设你拥有基本的版本控制的知识,了解subversion的基本用 ...

  5. 基于ELK的简单数据分析

    原文链接: http://www.open-open.com/lib/view/open1455673846058.html 环境 CentOS 6.5 64位 JDK 1.8.0_20 Elasti ...

  6. 【转】Hudson插件Email-Ext邮件模板时间格式化的解决方法

    原文地址:http://www.cnblogs.com/haycco/archive/2012/03/20/3031397.html 最近因对Hudson版本进行了升级为2.2.0,所以各方面都在搞项 ...

  7. github使用密钥登录

    注册github之后 初次使用git的用户要使用git协议大概需要三个步骤: 一.生成密钥对 二.设置远程仓库(本文以github为例)上的公钥     一.生成密钥对 再window系统中可以通过x ...

  8. iOS - 获取安装所有App的Bundle ID

    先导入#import <objc/runtime.h>头文件 使用runtime获取设备上的所有app的bundle id // Class LSApplicationWorkspace_ ...

  9. redis进程守护脚本

    #!/bin/bash redis_dir="/usr/local/redis" redis_conf="/usr/local/redis/redis.conf" ...

  10. redis -clock_gettime问题

    /home/wm/redis-/deps/jemalloc/src/nstime.c:: undefined reference to `clock_gettime' 这个错误 解决思路如下 .查找实 ...