Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 131 lines (109 sloc) 4.043 kB
1751691 @JacksonTian Update Readme
authored
1 这个世界上不存在所谓回调函数深度嵌套的问题。 —— Jackson Tian(http://weibo.com/shyvo)
188af67 @JacksonTian Update README.md
authored
2 ---
3
4 [![Build Status](https://secure.travis-ci.org/JacksonTian/eventproxy.png)](http://travis-ci.org/JacksonTian/eventproxy)
ea2f2aa @JacksonTian Update README.md
authored
5
feeeba5 @JacksonTian Update README.md
authored
6 ---
b81e4b6 @JacksonTian Update README.md
authored
7
9ecf9f6 @JacksonTian Update README.md
authored
8 [EventProxy API Documentation](http://eventproxy.html5ify.com/jsdoc/symbols/EventProxy.html)
9
10 npm install eventproxy
ea2f2aa @JacksonTian Update README.md
authored
11
12 ---
feeeba5 @JacksonTian Update README.md
authored
13 EventProxy.js仅仅是一个很轻量的工具,但是能够带来一种事件式编程的思维变化。有几个特点:
b81e4b6 @JacksonTian Update README.md
authored
14
f16e610 @JacksonTian Update readme
authored
15 1. 利用事件机制解耦复杂业务逻辑
16 2. 移除被广为诟病的深度callback嵌套问题
17 3. 将串行等待变成并行等待,提升多异步场景下的执行效率
9ecf9f6 @JacksonTian Update README.md
authored
18 4. 无平台依赖,适合前后端,能用于浏览器和Node.js
db0a02e @JacksonTian Merge branch 'master' of https://github.com/shyvo1987/eventproxy.js
authored
19
1751691 @JacksonTian Update Readme
authored
20 现在的,无深度嵌套的,并行的
6abc7f2 @JacksonTian Update by markdown.
authored
21
33f7326 @JacksonTian Update Readme
authored
22 var proxy = new EventProxy();
ed55c9a @JacksonTian Update Readme
authored
23 var render = function (template, data, l10n){
33f7326 @JacksonTian Update Readme
authored
24 _.template(template, data);
25 };
ed55c9a @JacksonTian Update Readme
authored
26 proxy.assign("template", "data", "l10n", render);
33f7326 @JacksonTian Update Readme
authored
27 $.get("template", function (template) {
ed55c9a @JacksonTian Update Readme
authored
28 // something
33f7326 @JacksonTian Update Readme
authored
29 proxy.trigger("template", template);
30 });
31 $.get("data", function (data) {
ed55c9a @JacksonTian Update Readme
authored
32 // something
33f7326 @JacksonTian Update Readme
authored
33 proxy.trigger("data", data);
34 });
ed55c9a @JacksonTian Update Readme
authored
35 $.get("l10n", function (l10n) {
36 // something
37 proxy.trigger("l10n", l10n);
38 });
6abc7f2 @JacksonTian Update by markdown.
authored
39
1751691 @JacksonTian Update Readme
authored
40 过去的,深度嵌套的,串行的。
6abc7f2 @JacksonTian Update by markdown.
authored
41
ed55c9a @JacksonTian Update Readme
authored
42 var render = function (template, data){
43 _.template(template, data);
44 };
45 $.get("template", function (template) {
46 // something
47 $.get("data", function (data) {
48 // something
49 $.get("l10n", function (l10n) {
50 // something
51 render(template, data);
52 });
53 });
54 });
6abc7f2 @JacksonTian Update by markdown.
authored
55
ba76e11 @JacksonTian Update README.md
authored
56 For Frontend user:
57
8ed0fd2 @JacksonTian Update the README
authored
58 Assign once. The callback will be executed once when all event were fired.
59
ba76e11 @JacksonTian Update README.md
authored
60 <script src="eventproxy.js"></script>
61 <script>
62 var proxy = new EventProxy();
63 var render = function (template, data, l10n){
64 _.template(template, data);
65 };
66 proxy.assign("template", "data", "l10n", render);
67 $.get("template", function (template) {
68 // something
69 proxy.trigger("template", template);
70 });
71 $.get("data", function (data) {
72 // something
73 proxy.trigger("data", data);
74 });
75 $.get("l10n", function (l10n) {
76 // something
77 proxy.trigger("l10n", l10n);
78 });
79 </script>
80
8ed0fd2 @JacksonTian Update the README
authored
81 Assign always. The callback will be executed first time when all event were fired. And after that, any event was fired will trigger callback. It's useful when you need refresh UI with newest data, e.g. stock app.
82
83 <script src="eventproxy.js"></script>
84 <script>
85 var proxy = new EventProxy();
86 var render = function (template, data, l10n){
87 _.template(template, data);
88 };
89 proxy.assignAll("template", "dataUpdate", "l10n", render);
90 $.get("template", function (template) {
91 // something
92 proxy.trigger("template", template);
93 });
94
95 $.get("l10n", function (l10n) {
96 // something
97 proxy.trigger("l10n", l10n);
98 });
99
100 // Need refresh data and UI for some realtime application.
101 setInterval(function () {
102 $.get("data", function (data) {
103 // something
104 proxy.trigger("dataUpdate", data);
105 });
106 }, 1000);
107 </script>
108
9ecf9f6 @JacksonTian Update README.md
authored
109 For Node.js:
6cbbc11 @JacksonTian Update README.md
authored
110
ba76e11 @JacksonTian Update README.md
authored
111 var EventProxy = require("eventproxy.js").EventProxy;
112
113 var proxy = new EventProxy();
114 var render = function (template, data, l10n){
115 return _.template(template, data);
116 };
117 proxy.assign("template", "data", "l10n", render);
118 $.get("template", function (template) {
119 // something
120 proxy.trigger("template", template);
121 });
122 $.get("data", function (data) {
123 // something
124 proxy.trigger("data", data);
125 });
126 $.get("l10n", function (l10n) {
127 // something
128 proxy.trigger("l10n", l10n);
129 });
cf70513 @JacksonTian Edited README.md via GitHub
authored
130
Something went wrong with that request. Please try again.