Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ch 9 example start

  • Loading branch information...
commit 532c51d88adfaaf486450e6ea6a4d804c782db08 1 parent 9965c7c
@benvinegar benvinegar authored
View
36 static/sdk/sdk.js
@@ -0,0 +1,36 @@
+(function(window, undefined) {
+ var Stork = {};
+
+ var listeners = {};
+
+ Stork.listen = function(eventName, handler) {
+ if (typeof listeners[eventName] === 'undefined')
+ listeners[eventName] = [];
+
+ listeners[eventName].push(handler);
+ };
+
+ Stork.unlisten = function(eventName, handler) {
+ if (!listeners[eventName])
+ return;
+
+ for (var i = 0; i < listeners[eventName].length; i++) {
+ if (listeners[eventName][i] === handler) {
+ listeners.splice(i, 1);
+ break;
+ }
+ }
+ };
+
+ Stork.broadcast = function(eventName) {
+ if (!listeners[eventName])
+ return;
+
+ for (var i = 0; i < listeners[eventName].length; i++) {
+ listeners[eventName][i]();
+ }
+ };
+
+
+ window.Stork = Stork;
+})(this);
View
36 templates/09/event-hooks/hooks.js
@@ -0,0 +1,36 @@
+(function(window, undefined) {
+ var Stork = {};
+
+ var listeners = {};
+
+ Stork.listen = function(eventName, handler) {
+ if (typeof listeners[eventName] === 'undefined')
+ listeners[eventName] = [];
+
+ listeners[eventName].push(handler);
+ };
+
+ Stork.unlisten = function(eventName, handler) {
+ if (!listeners[eventName])
+ return;
+
+ for (var i = 0; i < listeners[eventName].length; i++) {
+ if (listeners[eventName][i] === handler) {
+ listeners.splice(i, 1);
+ break;
+ }
+ }
+ };
+
+ Stork.broadcast = function(eventName) {
+ if (!listeners[eventName])
+ return;
+
+ for (var i = 0; i < listeners[eventName].length; i++) {
+ listeners[eventName][i]();
+ }
+ };
+
+
+ window.Stork = Stork;
+})(this);
View
10 templates/09/event-hooks/index.html
@@ -0,0 +1,10 @@
+{% extends 'example.html' %}
+
+{% block content %}
+
+ <script src="hooks.js"></script>
+
+ <h3>Event Hooks<h3/>
+
+
+{% endblock %}
View
13 templates/09/versioning/index.html
@@ -0,0 +1,13 @@
+{% extends 'example.html' %}
+
+{% block content %}
+
+ <h3>Versioning<h3/>
+
+ <script src="sdk.js"></script>
+ <script>
+ Stork.init('1.0', function(S) {
+ debugger;
+ });
+ </script>
+{% endblock %}
View
40 templates/09/versioning/lib.1_0.js
@@ -0,0 +1,40 @@
+(function(window, undefined) {
+ var public = {};
+
+ public.productWidget = function(id, domId) {
+
+ };
+
+ var listeners = {};
+
+ public.listen = function(eventName, handler) {
+ if (typeof listeners[eventName] === 'undefined')
+ listeners[eventName] = [];
+
+ listeners[eventName].push(handler);
+ };
+
+ public.unlisten = function(eventName, handler) {
+ if (!listeners[eventName])
+ return;
+
+ for (var i = 0; i < listeners[eventName].length; i++) {
+ if (listeners[eventName][i] === handler) {
+ listeners.splice(i, 1);
+ break;
+ }
+ }
+ };
+
+ public.broadcast = function(eventName) {
+ if (!listeners[eventName])
+ return;
+
+ for (var i = 0; i < listeners[eventName].length; i++) {
+ listeners[eventName][i]();
+ }
+ };
+
+
+ window.Stork.instances['1.0'] = public;
+})(this);
View
49 templates/09/versioning/sdk.js
@@ -0,0 +1,49 @@
+(function(window, undefined) {
+ var Stork = {};
+
+ Stork.instances = {};
+
+ function loadScript(url, callback) {
+ var script = document.createElement('script');
+
+ script.type = 'text/javascript';
+ script.async = true;
+ script.src = url;
+
+ var entry = document.getElementsByTagName('script')[0];
+ entry.parentNode.insertBefore(script, entry);
+
+ if (script.addEventListener)
+ script.addEventListener('load', callback, false);
+ else {
+ script.attachEvent('onreadystatechange', function onready() {
+ if (/complete|loaded/.test(script.readyState)) {
+ callback();
+
+ script.detachEvent('onreadystatechange', onready);
+ }
+ });
+ }
+ }
+ Stork.init = function(version, callback) {
+ // If no version string is given (function callback is
+ // first argument)
+ if (typeof version === 'function') {
+ callback = version;
+ version = '1.1';
+ }
+
+ var file;
+ switch (version) {
+ case '1.0': file = 'lib.1_0.js'; break;
+ case '1.1': file = 'lib.1_1.js'; break;
+ default:
+ throw "Unknown SDK version: " + version;
+ }
+
+ loadScript(file, function() {
+ callback(Stork.instances[version]);
+ });
+ };
+ window.Stork = Stork;
+}(this));
View
8 templates/09/web-service-apis/index.html
@@ -0,0 +1,8 @@
+{% extends 'example.html' %}
+
+{% block content %}
+
+ <h3>Web Service APIs<h3/>
+
+
+{% endblock %}
View
7 templates/index.html
@@ -47,5 +47,12 @@
<li><a href="/06/auth-new-window/index.html">Setting Cookies in a New Window</a></li>
</ul>
</li>
+ <li>
+ <h3>Chapter 9</h3>
+ <ul>
+ <li><a href="/09/event-hooks/index.html">Event Hooks</a></li>
+ <li><a href="/09/versioning/index.html">Versioning</a></li>
+ <li><a href="/09/web-service-apis/index.html">Web Service APIs</a></li>
+ </ul>
</ul>
{% endblock %}
Please sign in to comment.
Something went wrong with that request. Please try again.