[AngularJS + cryptoJS + Gravatar] Provider vs factory
Configurable Bits Need a Provider
We want to be able to configure the characterLength
before Tweetable
runs. Refactor the Tweetable
factory into a provider and expose asetLength()
function that will allow us to set a characterLength
in our app config.
angular.module('NoteWrangler') .factory('Tweetable', ['$http', function TweetableFactory($http) { var characterLength = 144; return function(potentialTweet) { return $http({ method: 'POST', url: 'http://gentle-spire-1153.herokuapp.com/tweet', data: { description: potentialTweet, maxLength: characterLength } }); }; }]);
Change the factory definition into a provider definition.
.provider('Tweetable', ['$http', function TweetableProvider($http) {
Wrap the existing function returned by our TweetableProvider()
function in a call to the $get()
function required by providers. Don't forget to move the $http
service injection!
angular.module('NoteWrangler') .provider('Tweetable', [function TweetableProvider() { var characterLength = 144; this.$get = function($http){ return function(potentialTweet) { return $http({ method: 'POST', url: 'http://gentle-spire-1153.herokuapp.com/tweet', data: { description: potentialTweet, maxLength: characterLength } }); }; }; }]);
Create a setLength()
function attached to the provider that sets thecharacterLength
variable.
angular.module('NoteWrangler') .provider('Tweetable', [function TweetableProvider() { var characterLength = 144; this.$get = function($http){ return function(potentialTweet) { return $http({ method: 'POST', url: 'http://gentle-spire-1153.herokuapp.com/tweet', data: { description: potentialTweet, maxLength: characterLength } }); }; }; this.setLength = function(length){ characterLength = length; }; }]);
Configuring the Tweet Length
Now that our provider is ready to go, let's call the setLength()
method ofTweetableProvider
to configure the acceptable maximum tweet length. Instead of 144 characters, we need to allow for a characterLength
of 40.
Let's call config()
on our NoteWrangler
module and provide it an anonymous function.
Inject TweetableProvider
into the config()
function.
Call the setLength()
function of TweetableProvider
from within the config()
function and pass it a value of 40.
angular.module('NoteWrangler', ['ngRoute']) .config(function(TweetableProvider){ TweetableProvider.setLength(40); });
Link: https://code.google.com/p/crypto-js/
相关文章
- vs2005注册_vs注册界面设计
- 一文吃透 VS Code+Git 操作(vs code中git的相关配置与使用)
- Flink教程(30)- Flink VS Spark[通俗易懂]
- [工具推荐] VS code网页开发插件Live Preview [实时预览html]
- Go语言中常见100问题-#22 Being confused about nil vs. empty slices
- GreatSQL vs MySQL性能测试来了,速围观~
- 教你Visual Studio Code(VS Code)如何查询PostgreSQL拓展安装教程图解
- Go语言框架:Beego vs Gin 的区别详解编程语言
- VS如何连接MySQL数据库(vs怎么连接mysql)
- 如何使用VS连接MySQL(vs怎么连接mysql)
- VS连接Oracle数据库:一步搞定(vs连oracle数据库)
- MySQL vs 替代:哪种更好?(mysqlor替代)
- 开源 vs. 闭源
- VS中使用MySQL:获取最佳性能(vs和mysql)
- VS 开发 SQL Server 数据库应用实践(vs的sqlserver)
- VS整合SQLServer,实现数据库管理无忧(vs和sqlserver)
- VS开发环境再无Redis踪影(vs中没有redis了)
- 研究 VS C 如何从 Redis 中读取数据(vs c 读取redis)
- VS控制台操作Redis一个非常简单的新手指南(vs控制redis)
- Redis vs 源码分析比较为何(vs redis源码分析)
- Oracle vs Ash抢占数据库市场的双雄之争(oracle v ash)