一、概述

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的更多相关文章

  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. Ember.js学习教程 -- 目录

    写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...

  4. Orchard入门:如何创建一个完整Module

    这是一个Orchard-Modules的入门教程.在这个教程里,我们将开发两个功能页面分别用于数据录入与数据展示. 完成上述简单功能开发,我们一共需要6个步骤.分别为: 创建Module 创建Mode ...

  5. Learning storm book 笔记8-Log Processing With Storm

    有代码的书籍看起来就是爽,看完顺便跑个demo,感觉很爽! 场景分析 主要是利用apache的访问日志来进行分析统计 如用户的IP来源,来自哪个国家或地区,用户使用的Os,浏览器等信息,以及像搜索的热 ...

  6. 【原创】Odoo开发文档学习之:ORM API接口(ORM API)(边Google翻译边学习)

    官方ORM API开发文档:https://www.odoo.com/documentation/10.0/reference/orm.html Recordsets(记录集) New in vers ...

  7. 7.3 Models -- Creating And Deleting Records

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

  8. 7.7 Models -- Working with Records

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

  9. Models

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

随机推荐

  1. 原生js--HTTP进度事件

    1.HTTP进度事件属于XHR2规范定义的系列事件 2.事件模型中会触发不同的事件,所以不再需要检查readyState事件 3.当调用send()时,触发loadstart事件 4.当正在加载服务器 ...

  2. 使用SQLite3工具查看sqlite.db文件

    http://www.sqlite.org OS X自从10.4后把SQLite这套相当出名的数据库软件,放进了作业系统工具集里.OS X包装的是第三版的SQLite,又称SQLite3.这套软件有几 ...

  3. call()、apply()和bind()的异同

    相同点: 改变this的指向: var a = { name:"丸子", fn:function(){ console.log(this.name); } } var b = a. ...

  4. LeetCode 43 Multiply Strings(字符串相乘)

    题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description     求解大数相乘问题   按照上图所示,进行嵌套循环计算 ...

  5. vue Element-UI组件

    一.UI组件 目的: 提高开发效率, 别人提供好一切, 拿过来直接用饿了么团队开源一个基于vue组件库 Element-UI ==> pc端 文档: http://element-cn.elem ...

  6. 以太网端口二种链路类型:Access 和Trunk

    Access 类型的端口:只能属于1 个VLAN,一般用于连接计算机的端口:    Trunk 类型的端口:可以允许多个VLAN 通过,可以接收和发送多个VLAN 的报文,一般用于交换机之间连接的端口 ...

  7. CentOS7下Elastic Stack 5.0日志分析系统搭建

    原文链接:http://www.2cto.com/net/201612/572296_3.html 在http://localhost:5601下新建索引页面输入“metricbeat-*”,之后ki ...

  8. backBone.js初识

    一.单页面应用 1.单页面应用(single-page application :SPA),是指在浏览器中运行的应用,在使用期间不会重新加载页面. 2.它所有的活动局限于一个Web页面,仅在初始化加载 ...

  9. 转基于概率的矩阵分解原理详解(PMF)

    上一篇博客讲到了推荐系统中常用的矩阵分解方法,RegularizedMF是对BasicMF的优化,而PMF是在RegularizedMF的基础上,引入概率模型进一步优化.假设用户U和项目V的特征矩阵均 ...

  10. 2017年TOP100summit开幕在即, 15位大咖担任联席主席甄选最值得学习的100个研发案例

    从万维网到物联网,从信息传播到人工智能,20年间软件研发行业趋势发生了翻天覆地的变化.大数据.云计算.AI等新兴领域逐渐改变我们的生活方式,Devops.容器.深度学习.敏捷等技术方式和工作理念对软件 ...