Permalink
Browse files

* datatype.js: latest changes, name change and adopting ruby style

  • Loading branch information...
1 parent 01496e9 commit c8c95796451bb79e0888c0bc210a4043910be59e David Nolen committed May 19, 2010
Showing with 23 additions and 15 deletions.
  1. +8 −5 datatype.js
  2. +15 −10 readme.textile
View
@@ -8,7 +8,7 @@
_.mixin({
// check if an object is using a module
- includes: function(x, modules) {
+ includes: function(x, module) {
return _(x.modules).indexOf(module) != -1;
},
@@ -17,7 +17,8 @@
return x instanceof Module;
},
- // create grouping of functions for mixins
+ // create grouping of functions. Can be used with classes
+ // to combine functionality.
Module: function(obj) {
obj.prototype = new Module;
obj.toString = function() {
@@ -36,18 +37,19 @@
return x._class = _class;
},
- // creates a class. does not support inheritance for good reason
- // use Modules. Selectors will be generated for any methods defined
- // in your class
+ // creates a class. does not support inheritance because inheritance
+ // and AJAX do not work well together. Use Modules instead.
Class: function(obj) {
obj = obj || {};
+
var klass = function() {
if(_.isFunction(this.initialize)) {
return this.initialize.apply(this, arguments);
} else {
return this;
}
};
+
var modules = obj.includes || [],
name = obj.name = (obj.name || _.uniqueId("UnnamedClass")),
methodMap = modules.map(function(x) {
@@ -61,6 +63,7 @@
return ["<Class: ", name, ">"].join("");
}
});
+
klass.name = name;
klass.prototype = new Class();
klass.prototype.type = klass;
View
@@ -1,4 +1,4 @@
-h1. Introduction to datatype.js
+h1. Introduction to Underoop.js
h2. Defining new types
@@ -19,10 +19,10 @@ MyClass.prototype.woz = function() {
}
</pre>
-Using datatype.js you can instead write the following:
+Using Underoop.js you can instead write the following:
<pre>
-var MyClass = _.Type({
+var MyClass = _.Class({
initialize: function() {
this.foo = "foo";
this.bar = "bar";
@@ -36,12 +36,12 @@ var MyClass = _.Type({
});
</pre>
-h2. Protocols
+h2. Modules
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:
<pre>
-var Events = _.Protocol({
+var Events = _.Module({
addEvent: function(type, fn) { ... },
removeEvent: function(type, fn) { ... },
removeEvents: function(type) { ... },
@@ -52,28 +52,30 @@ var Events = _.Protocol({
And "mixin" in this functionality easily:
<pre>
-var Foo = _.Type({
- protocols: [Events],
+var Foo = _.Module({
+ includes: [Events],
intialize: function () {
...
}
});
</pre>
+This is similar to how Ruby deals with this.
+
h2. Messaging
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.
<pre>
-var Foo = _.Type({
+var Foo = _.Class({
initialize: function(z) {
this.z = z;
},
add: function(x, y) {
return [x, y].join("");
}
});
-var Bar = _.Type({
+var Bar = _.Class({
initialize: function(z) {
this.z = z;
},
@@ -86,4 +88,7 @@ var a = new Foo("cool!");
var b = new Bar(6);
_([a, b]).map(_sel.add(4, 5));
-</pre>
+</pre>
+
+h2. Inheritance
+

0 comments on commit c8c9579

Please sign in to comment.