7.5 Models -- Persisting Records
一、概述
1. 在Ember Data上以每个实例为基础,records被持久化。在DS.Model的任何一个实例上调用save()并且它将产生一个网络请求。
2. 下面是一些例子:
var post = store.createRecord('post', {
title: 'Rails is Omakase',
body: 'Lorem ipsum'
});
post.save(); // => POST to '/posts'
store.findRecord('post', 1).then(function(post) {
post.get('title'); // => "Rails is Omakase"
post.set('title', 'A new post');
post.save(); // => PUT to '/posts/1'
});
二、Promises
1. save()返回一个promise,所以它是非常容易处理成功和失败的情况的。这里是一个普遍的模式:
var post = store.createRecord('post', {
title: 'Rails is Omakase',
body: 'Lorem ipsum'
});
var self = this;
function transitionToPost(post) {
self.transitionToRoute('posts.show', post);
}
function failure(reason) {
// handle the error
}
post.save().then(transitionToPost).catch(failure);
// => POST to '/posts'
// => transitioning to posts.show route
2. promises甚至使处理失败的网络请求变得容易:
var post = store.createRecord('post', {
title: 'Rails is Omakase',
body: 'Lorem ipsum'
});
var self = this;
var onSuccess = function(post) {
self.transitionToRoute('posts.show', post);
};
var onFail = function(post) {
// deal with the failure here
};
post.save().then(onSuccess, onFail);
// => POST to '/posts'
// => transitioning to posts.show route
3. 在这里 here你可以学到更多关于promises,但是这里是另外一个关于展示如何重试持久化的例子:
function retry(callback, nTimes) {
// if the promise fails
return callback().catch(function(reason) {
// if we haven't hit the retry limit
if (nTimes > 0) {
// retry again with the result of calling the retry callback
// and the new retry limit
return retry(callback, nTimes - 1);
}
// otherwise, if we hit the retry limit, rethrow the error
throw reason;
});
}
// try to save the post up to 5 times
retry(function() {
return post.save();
}, 5);
7.5 Models -- Persisting Records的更多相关文章
- 7.6 Models -- Finding Records
Ember Data的store为检索一个类型的records提供一个接口. 一.Retrieving a single record(检索单记录) 1. 通过type和ID使用store.findR ...
- 7.4 Models -- Pushing Records into the Store
一.概述 1. store是作为一个所有records的缓存,这些records已经被你的应用程序加载.在你的app中如果你的路由或者一个controller请求一条record,如果它在缓存中这个s ...
- Ember.js学习教程 -- 目录
写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...
- Orchard入门:如何创建一个完整Module
这是一个Orchard-Modules的入门教程.在这个教程里,我们将开发两个功能页面分别用于数据录入与数据展示. 完成上述简单功能开发,我们一共需要6个步骤.分别为: 创建Module 创建Mode ...
- Learning storm book 笔记8-Log Processing With Storm
有代码的书籍看起来就是爽,看完顺便跑个demo,感觉很爽! 场景分析 主要是利用apache的访问日志来进行分析统计 如用户的IP来源,来自哪个国家或地区,用户使用的Os,浏览器等信息,以及像搜索的热 ...
- 【原创】Odoo开发文档学习之:ORM API接口(ORM API)(边Google翻译边学习)
官方ORM API开发文档:https://www.odoo.com/documentation/10.0/reference/orm.html Recordsets(记录集) New in vers ...
- 7.3 Models -- Creating And Deleting Records
一.Creating 1. 你可以通过调用在store中的createRecord方法来创建records. store.createRecord('post', { title: 'Rails is ...
- 7.7 Models -- Working with Records
Modifying Attributes 1. 一旦一条record被加载,你可以开始改变它的属性.在Ember.js对象中属性的行为就像正常的属性.作出改变就像设置你想要改变的属性一样简单: var ...
- Models
Models Models control the data source, they are used for collecting and issuing data, this could be ...
随机推荐
- jQuery Sizzle选择器(三)
在Sizzle的入口方法Sizzle()中看到的一个根据浏览器来初始化document各个方法的函数setDocument(),接下来主要看一下这个方法都做了什么. 但之前有必要看一下它用到的一些Si ...
- UVA 1335 Beijing Guards(二分答案)
入口: https://cn.vjudge.net/problem/UVA-1335 [题意] 有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个 ...
- 【NOI2015】荷马史诗[Huffman树+贪心]
#130. [NOI2015]荷马史诗 统计 描述 提交 自定义测试 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读 ...
- cJson 创建 读取
关于c语言操作json,cjson还挺好用,许多操作已经帮开发员封装好了,使用起来很方便.资源下载地址为:http://sourceforge.net/projects/cjson/在test.c文件 ...
- virgo-tomcat-server最大并发连接数的修改
首先,我们如果需要修改tomcat 7的最大连接数,我们可以去tomcat官方网站,查看Documentation 进入tomcat的官方网站http://tomcat.apache.org我们点击左 ...
- android 设置系统屏幕亮度
/** * 获得当前屏幕亮度的模式 * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度 * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为 ...
- 专访|HPE测试中心总监徐盛:测试新思维-DevOps,持续测试,更敏捷,更快速
2016年7月22日,「HPE&msup软件技术开放日」将在上海浦东新区,张江高科技园区纳贤路799号科荣大厦小楼2楼举办,msup携手HPE揭秘全球测试中心背后的12条技术实践. 徐盛:HP ...
- 推荐系统之最小二乘法ALS的Spark实现
1.ALS算法流程: 初始化数据集和Spark环境----> 切分测试机和检验集------> 训练ALS模型------------> 验证结果-----------------& ...
- 【转】JavaScript中的匿名函数及函数的闭包
对闭包理解一直不甚明了,在此特转摘博文一篇以备查用. 原文地址:http://www.cnblogs.com/rainman/archive/2009/05/04/1448899.html 相关文章: ...
- 2018-2019-2 20165330《网络对抗技术》Exp1 PC平台逆向破解
目录 实验目标 实验内容 知识点描述 实验步骤 实验过程中遇到的问题 实验感想 实验目标 本次实验的对象是一个名为pwn1的linux可执行文件. -该程序正常执行流程是:main调用foo函数,fo ...