[AngularJS] Best Practise - Resolve promises in router, defer controllers
See more:http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/
/**
* Created by Answer1215 on 1/13/2015.
*/ /**
* Controller(s)
* */ function AppController($rootScope, $location) { var appCtrl = this; $rootScope.$on('$routeChangeError', function(event, current, previous, rejection) {
appCtrl.message = rejection.message;
appCtrl.isError = true;
}) appCtrl.goHome = function() { $location.path('/');
}
} /**
* Directive(s)
* */ function ErrorDirective() {
return {
restrict: "EA",
bindToController: true,
controller: 'AppController',
controllerAs: 'appCtrl',
templateUrl: 'error.html'
}
} /**
* Angular module
* */ angular.module('app', [
'ngRoute',
'app.home'
])
.constant('PEOPLE_URL', 'data.json')
.controller('AppController', AppController)
.directive('error', ErrorDirective)
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "home.html",
controller: "HomeController",
controllerAs: "homeCtrl",
/*
* resolve run first, prepare the data ready,
* then controller & template show up
* */
resolve: HomeController.resolve
});
});
/**
* Created by Answer1215 on 1/13/2015.
*/ function HomeController(loadPeople, getMessage) {
var homeCtrl = this; homeCtrl.message = getMessage;
homeCtrl.people = loadPeople;
} HomeController.resolve = { loadPeople: function(SimpleDataService) {
return SimpleDataService.getPeople();
},
getMessage : function(SimpleDataService) {
return SimpleDataService.message;
}
} function SimpleDataService($http, $q, PEOPLE_URL) {
var simpleData = {};
simpleData.people = [];
simpleData.message = "Hello From Service"; simpleData.getPeople = function() {
return fetchPeople().then(function(result) {
return result.data;
}); } function fetchPeople() {
var defer = $q.defer();
$http.get(PEOPLE_URL)
.success(function(data, status, headers, config) {
simpleData.people = data;
defer.resolve({data: data, message: "OK"});
}).
error(function(data, status, headers, config) { if(status === 404){
data = data;
defer.reject({data: data, message: "Cannot find " + config.url});
}
});
return defer.promise;
} return simpleData;
} angular.module('app.home', [])
.controller('HomeController', HomeController)
.service('SimpleDataService', SimpleDataService);
While the routes are being resolved we want to show the user something to indicate progress. Angular will fire the $routeChangeStart
event as we navigate away from the page, which we can show some form of loading and ajax spinner, which can then be removed on the $routeChangeSuccess
[AngularJS] Best Practise - Resolve promises in router, defer controllers的更多相关文章
- AngularJS ui-router 用resolve、service预先加载数据写法,属于优化性能方面吧
AngularJS的service怎么声明此处就不再赘述,下面的例子是ui-router中使用service的实现代码 $stateProvider.state('myState', { url: & ...
- [AngularJS] Best Practise - Controller
ControllerAs: Use thecontrollerAs syntax always as it aids in nested scoping and controller instance ...
- [AngularJS] Best Practise - Minification and annotation
Annotation Order: It's considered good practice to dependency inject Angular's providers in before o ...
- [AngularJS] Best Practise - Module
Module definitions Angular modules can be declared in various ways, either stored in a variable or u ...
- 转: angular编码风格指南
After reading Google's AngularJS guidelines, I felt they were a little too incomplete and also guide ...
- AngularJS之高级Route【三】(八)
前言 我们知道默认的路由提供(Route Provider)在复杂的应用程序中是不太适合应用场景,它存在诸多限制,所以在Angular 1.2之后此时我们不得不将路由提供作为一个单独的模块当我们需要使 ...
- angularJS中的Promise对象($q)的深入理解
原文链接:a better way to learn AngularJS - promises AngularJS通过内置的$q服务提供Promise编程模式.通过将异步函数注册到promise对象, ...
- Promise in AngularJS
What's promise Angular’s event system provides a lot of power to our Angular apps. One of the most p ...
- 转AngularJS路由插件
AngularJS学习笔记--002--Angular JS路由插件ui.router源码解析 标签: angular源码angularjs 2016-05-04 13:14 916人阅读 评论(0) ...
随机推荐
- mysql 共享锁-排它锁
转 InnoDB 行级锁 http://www.cnblogs.com/dongqingswt/archive/2013/03/28/2987367.html InnoDB 行级锁 分类: 数据库2 ...
- php类的实现
zend_class_entry typedef struct _zend_class_entry zend_class_entry; struct _zend_class_entry { char ...
- chromium的部署工具depot_tools和gclient
depot_tools是个工具包,里面包含gclient.gcl.gn和ninja等工具.其中gclient是代码获取工具,它其实是利用了svn和git.主要涉及的depot_tools文件夹下的文件 ...
- apache开源项目--HIVE
Hive是一个基于Hadoop的数据仓库平台.通过hive,我们可以方便地进行ETL的工作.hive定义了一个类似于SQL的查询语言:HQL,能 够将用户编写的QL转化为相应的Mapreduce程序基 ...
- 【开源专访】Sea.js创始人玉伯的前端开发之路
摘要:玉伯,淘宝前端类库 KISSY.前端模块化开发框架SeaJS.前端基础类库Arale的创始人.本期[开源专访]我们邀请玉伯来为我们分享一些关于前端框架.前端开发的那些事,以及前端大牛是如何炼成的 ...
- (转载)C++lambda表达式
(转载)http://www.cnblogs.com/L-hq815/archive/2012/08/23/2653002.html lambda表达式 C++ 语言中的lambda表达式在很多情况下 ...
- Android Fragment学习(一)
说明 Fragment是在Android3.0(即API 11)才出现的,如果在之前的版本要使用,需要添加support库. Fragment可以认为是Actvity模块化的组件,可以很方便地被添加, ...
- 数列极限---和Gauss(取整)函数有关
- [Andrew]Ext.net前台弹框
//有询问的提示框 Ext.Msg.show({ title: title, msg: msg, buttons: Ext.Msg.Y ...
- REST API TESTING
在敏捷开发过程中 每隔两周就是一个sprint,,, 在上个sprint中,任务就是REST API TESTING 因为以前没做过API 测试,不懂,然后经过询问查找 终于知道,需要发送请求,然后获 ...