Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* datatype.js: latest

  • Loading branch information...
commit 494ffbf23bf5c6952c981090beaff5c74ce5cd41 1 parent e483e6b
David Nolen authored

Showing 2 changed files with 40 additions and 37 deletions. Show diff stats Hide diff stats

  1. +19 15 datatype.js
  2. +21 22 readme.textile
34 datatype.js
... ... @@ -1,6 +1,8 @@
1 1 (function() {
2 2
3   - var multiMap = {};
  3 + var root = this;
  4 + root._sel = {};
  5 +
4 6 function Type() {};
5 7 function Protocol() {};
6 8
@@ -9,20 +11,6 @@
9 11 return _(x.protocols).indexOf(protocol) != -1;
10 12 },
11 13
12   - Multi: function(name, type, fn) {
13   - if(_.isUndefined(multiMap[name])) {
14   - var dispatchTable = {},
15   - dispatcher = {};
16   - multiMap[name] = dispatchTable;
17   - dispatcher[name] = function(instance) {
18   - var rest = _.rest(arguments);
19   - return multiMap[name][instance.name].apply(instance, rest);
20   - };
21   - _.mixin(dispatcher);
22   - }
23   - multiMap[name][type.name] = fn;
24   - },
25   -
26 14 isProtocol: function() {
27 15 return x instanceof Protocol;
28 16 },
@@ -65,9 +53,25 @@
65 53 klass.prototype = new Type();
66 54 klass.prototype.type = klass;
67 55 klass.prototype.protocols = klass.protocols = protocols;
  56 +
68 57 _(obj).each(function(v, k) {
69 58 klass.prototype[k] = v;
  59 + if(_.isUndefined(_sel[k])) {
  60 + _sel[k] = function() {
  61 + var args = arguments;
  62 + return function(x) {
  63 + var result;
  64 + try{
  65 + result = x[k].apply(x, args);
  66 + } catch (err) {
  67 + throw Error([x.name, "does not implement", k, "or signature does not match"].join(" "));
  68 + }
  69 + return result;
  70 + };
  71 + };
  72 + }
70 73 });
  74 +
71 75 return klass;
72 76 }
73 77 });
43 readme.textile
Source Rendered
@@ -22,7 +22,7 @@ MyClass.prototype.woz = function() {
22 22 Using datatype.js you can instead write the following:
23 23
24 24 <pre>
25   -var MyClass = _.datatype({
  25 +var MyClass = _.Type({
26 26 initialize: function() {
27 27 this.foo = "foo";
28 28 this.bar = "bar";
@@ -41,7 +41,7 @@ h2. Protocols
41 41 Often you'll have a chunk of functionality that does not belong to a particular class. You can define a set of methods like so:
42 42
43 43 <pre>
44   -var Events = _.protocol({
  44 +var Events = _.Protocol({
45 45 addEvent: function(type, fn) { ... },
46 46 removeEvent: function(type, fn) { ... },
47 47 removeEvents: function(type) { ... },
@@ -52,7 +52,7 @@ var Events = _.protocol({
52 52 And "mixin" in this functionality easily:
53 53
54 54 <pre>
55   -var Foo = _.datatype({
  55 +var Foo = _.Type({
56 56 protocols: [Events],
57 57 intialize: function () {
58 58 ...
@@ -62,29 +62,28 @@ var Foo = _.datatype({
62 62
63 63 h2. Polymorphism
64 64
65   -datatype.js doesn't support datatype inheritance of any kind. However it is sometimes useful to reuse a name. For this purpose there is <code>_.multi</code>. You can use this like so:
  65 +Being able to package up a message is pretty useful, especially at cutting down the amount of typing that you have to do. All methods of a Type are added to <code>_sel</code> so you use them with Underscore's excellent interfaces to the JavaScript collections.
66 66
67 67 <pre>
68   -var Foo = _.datatype();
69   -var Bar = _.datatype();
70   -
71   -_.multi("cool", Foo, function(x) {
72   - console.log("got type Foo!");
  68 +var Foo = _.Type({
  69 + initialize: function(z) {
  70 + this.z = z;
  71 + },
  72 + add: function(x, y) {
  73 + return [x, y].join("");
  74 + }
73 75 });
74   -
75   -_.multi("cool", Bar, function(x) {
76   - console.log("got type Bar!");
  76 +var Bar = _.Type({
  77 + initialize: function(z) {
  78 + this.z = z;
  79 + },
  80 + add: function(x, y) {
  81 + return x + y + thiz.z;
  82 + }
77 83 });
78 84
79   -var a = new Foo();
80   -var b = new Bar();
81   -
82   -_.cool(a);
83   -_.cool(b);
84   -</pre>
85   -
86   -With the above code the following also works great:
  85 +var a = new Foo("cool!");
  86 +var b = new Bar(6);
87 87
88   -<pre>
89   -_([a, b]).map(_.cool);
  88 +_([a, b]).map(_sel.add(4, 5));
90 89 </pre>

0 comments on commit 494ffbf

Please sign in to comment.
Something went wrong with that request. Please try again.