[Angular-Scaled Web] 9. Control your promises with $q
Learn how to manually control how asynchronous requests are handled with the use of promises. Because $http is built to work with promises, we saw a foreshadow of them in the previous lesson. We will take this a step further but seeing how to manually create a promise and then resolve or reject it as we see fit.
angular.module('eggly.models.categories', [
])
.service('CategoriesModel', function ($http, $q) {
var CategoriesModel = {},
URLS = {
FETCH: 'data/categories.json'
},
categories;
function extract(result) {
return result.data;
}
function cacheCategories(result) {
categories = extract(result);
return categories;
}
CategoriesModel.getCategories = function() {
return (categories) ? $q.when(categories) : $http.get(URLS.FETCH).then(cacheCategories);
};
CategoriesModel.getCategoryByName = function(categoryName) {
function findCategory(){
return _.find(categories, function(c){
return c.name == categoryName;
})
}
return $q(function(resolve, reject) {
//resolve it when categories are set
if(categories){
resolve(findCategory());
}else{
//if not set, get the categories
CategoriesModel.getCategories()
.then(function() {
resolve(findCategory());
})
}
})
};
return CategoriesModel;
})
;
[Angular-Scaled Web] 9. Control your promises with $q的更多相关文章
- 混合开发 Hybird Ionic Angular Cordova web 跨平台 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 创建一个web user control
1.创建文件 添加,然后选择web user control 2.添加控件 在工具栏搜索button,然后拖动三个button上去 <%@ Control Language="C#&q ...
- Docs-->.NET-->API reference-->System.Web.UI-->Control-->Methods-->FindControl
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.control.findcontrol?view=netframework-4.7 ...
- vue,react,angular三大web前端流行框架简单对比
常用的到的网站 vue学习库: https://github.com/vuejs/awesome-vue#carousel (json数据的格式化,提高本地测试的效率) json在线编辑: http: ...
- web api control注册及重写DefaultHttpControllerSelector、ApiControllerActionSelector、ApiControllerActionInvoker
namespace EWorkpal.WebApi { public class HttpNotFoundDefaultHttpControllerSelector : DefaultHttpCont ...
- angular模拟web API
现象:angular Cannot find module 'angular-in-memory-web-api'报错找不动“angular-in-memory-web-api”模块 解决:1.控制台 ...
- web api control注册及重写DefaultHttpControllerSelector、ApiControllerActionSelector、ApiControllerActionInvoker(转)
出处:http://www.cnblogs.com/kingCpp/p/4651154.html namespace EWorkpal.WebApi { public class HttpNotFou ...
- angular form set dynamic control(form动态设置control)
实现效果 form表单控件的实时更新 效果如图 关键代码 validateForm: FormGroup; // 表单校验 constructor( private fb: FormBuilder ) ...
- Angular+ionic2 web端 启动程序出现短暂 白屏或黑屏 的处理小妙招
在ionic2项目启动是会出现短暂的白屏或者黑屏,虽然很短,但是用户体验不太好.上网查了相关的资料,都是针对打包APP的解决办法,针对浏览器端使用的项目没有效果,所以自己写了样式,巧妙的避开这个问题, ...
随机推荐
- POJ 2362 Square
题意:给n个木棍,问能不能正好拼成一个正方形. 解法:POJ1011的简单版……不需要太多剪枝……随便剪一剪就好了……但是各种写屎来着QAQ 代码: #include<stdio.h> # ...
- SQL SERVER 实现分组合并实现列数据拼接
需求场景: SQL SERVER 中组织的数据结构是一个层级关系,现在需要抓出每个组织节点以上的全部组织信息,数据示例如下: ADOrg_ID--------------ParentID------- ...
- Android之布局属性
1) 布局的相关属性 ① android:layout_weight="1.0",layout_weight 用于给一个线性布局中的诸多视图重要度赋值.所有的视图都有一个layou ...
- [BILL WEI] stimulsoft 分组页眉页脚的使用
我们在通过stimulsoft设计报表的时候,有的时候,需要做出如下图报表样式 这个时候,因为箱号是分开扩展的,我们就需要用到分组页眉了,如下图demo跟实例所示:
- 在ASP.NET下做了一个实验MVC的小东西
星期五下班前一时兴起,对MVC发表了一点看法.后来就想干脆弄个小东西来验证一些自己的想法帮组理清思路.所要源代码的免了,3个小时的急就章实在是乱得可以,既没有好的架构,也没有任何代码质量可言,主要是实 ...
- bzoj 2818 Gcd(欧拉函数 | 莫比乌斯反演)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2818 [题意] 问(x,y)为质数的有序点对的数目. [思路一] 定义f[i]表示i之 ...
- 配置youcompleteme碰到的问题
Q1: 进入vim里面后,可以使用ycm的相关命令来看到底出现啦什么问题? :Ycm YcmCompleter YcmForceCompileAndDiagnostics YcmToggleLogs ...
- mysql 查看锁表解锁
-- 查看那些表锁到了show OPEN TABLES where In_use > 0;-- 查看进程号show processlist;--删除进程 kill 42236:
- 使用logback.xml配置来实现日志文件输出
转自:http://sungang-1120.iteye.com/blog/2104296 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback- ...
- Spark的应用程序
Spark的应用程序,分为两部分:Spark driver 和 Spark executor.