一、概述

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. ios-toolchain-based-on-clang-for-linux

    https://github.com/tpoechtrager/cctools-port.git https://www.embtoolkit.org

  2. LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)

    题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description   在字符 ...

  3. [原]secureCRT 改变显示宽度

    1.首先全局设置:Options - Global Options - Terminal - Appearance - Maximumcolumns 最大只能设置成1024(推荐256),设置越大越占 ...

  4. vscode 使用sublime风格代码

    Monokai 主题 One Dark Pro主题   vscode设置字体 "editor.fontFamily": "MONACO, Consolas, 'Couri ...

  5. gcc6.3的安装

    author:headsen  chen date: 2018-10-12  15:11:35 1,环境:centos7.3 ,64位,内核 3.10 2,安装过程 #!/bin/bash yum i ...

  6. elk单台环境搭建

    一.简介1.核心组成ELK由Elasticsearch.Logstash和Kibana三部分组件组成:Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分 ...

  7. [工具] 知网(CNKI)文献下载工具

    https://github.com/amyhaber/cnki-downloader 用于免费搜索,下载CNKI上的各类文献资料

  8. windows本地启动tomcat闪退

    da开cmd, 进入tomcat所在目录的bin目录: 执行startup.bat 查看设置的环境变量是否正确:如果不正确则在windows中设置正确的相关环境变量即可:

  9. jfinal的model和record如何相互转化?

    一.model转record: Model类: 1. /** * Convert model to record. */public Record toRecord() { return new Re ...

  10. dubbo入门之微服务客户端服务端配置

    正常一个服务不会只做客户端或者只做服务端,一般的微服务都是服务与服务相互调用,那么,应该怎么配置呢?接着之前的dubbo入门之helloWorld,我们再改改配置,即可实现正常的微服务架构.与之前相比 ...