程序员 2014-10-21 一天的奇妙之旅
推荐:《精通Node.js开发》 《Angular.js视频详解》
今天是个大晴天,作为soho,午饭后一定要好好晒晒太阳。这是让所有!soho
的童鞋羡慕嫉妒恨的!
啊~ 我香浓的咖啡,嗅到就有coding的冲动。好了,散文做到这里了,还是coding要紧,因为这是我的快乐来源啊。
先看看有啥新资讯
npm 2.0 新用法
{
"name": "blog-npm-run-scripts",
"version": "1.0.0",
"description": "Show of the new npm run command",
"scripts": {
"gulp" : "./node_modules/bin/gulp",
"grunt" : "./node_modules/bin/grunt"
}
}
运行
# run 'dev' task with in project install grunt
$ npm run grunt -- dev
# run 'dev' task with in project install gulp
$ npm run gulp -- dev
有个angular chart 插件不错
step 1 安装
bower install angular-chart.js --save
step 2
<script src="bower_components/dist/angular-chart.js"></script>
<link rel="stylesheet" href="bower_components/dist/angular-chart.css">
step 3
<canvas id="bar" class="chart chart-bar" data="data"
labels="labels"></canvas>
angular.module("app", ["chart.js"]).controller("BarCtrl", function ($scope) {
$scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
$scope.series = ['Series A', 'Series B'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
});
效果:
继续完善domain框架了。
下面开始修改 src/Repository.js
Repository#get
值得注意的是这里用了两次 if(actor = this.getFromCache(id))
进行判断是否缓冲里有,
这是因为yield可以让语法看起来是同步的,其实内部是异步的,所以加这个判断是很必要的。
getFromCache(id){
return this[cache][id];
}
*get(id){
let actor;
if(actor = this.getFromCache(id)){
return actor;
}
let result = yield this.getFromSnapshot(id);
if(actor = this.getFromCache(id)){
return actor;
}
let err = result[0];
let snapshot = result[1];
let stream = result[2];
let snap = snapshot.data;
let history = stream.events;
actor = new this.Actor();
// mix aggregate object.
actor.loadSnap(snap);
var historyv = [];
history.forEach(function(e){
historyv.push(e.payload);
})
actor.loadEvents(historyv);
return actor;
}
Repository#create
方法是用来创建一个Actor的,并把它加入到cache缓冲里。
*create (data) {
let actor = new this.Actor(data);
let result = yield this.getFromSnapShot(actor.id);
let err = result[0];
let snapshot = result[1];
let stream = result[2];
err = (yield this.createSnapshot({
aggregateid:actor.id,
aggregate:this.Actor.name,
data:actor.json,
revision:stream.revision
}))[0];
if(err){
throw err;
}else{
this[cache][actor.id] = actor;
return actor;
}
}
接下来,需要把Saga功能也融于到Actor里,就完善了。
我心中还有个疑惑,为了更优雅,如何能让domain知道,所有问题都集中在 #apply
方法上。