Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Last go through the docs before releasing 0.3a; slightly improved Ass…
…et#place in dom.indesign.jsx
- Loading branch information
stdbrouw
committed
Nov 1, 2010
1 parent
7b670b6
commit 121f20c
Showing
16 changed files
with
138 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,47 @@ | ||
===================================== | ||
Common design patterns in Extendables | ||
===================================== | ||
====================== | ||
Common design patterns | ||
====================== | ||
|
||
For framework design issues currently under discussion, look at `issues in the GitHub issue tracker with a design label <http://github.com/stdbrouw/Extendables/issues/labels/design>`_. | ||
|
||
getter/setters | ||
============== | ||
|
||
Sometimes, when coding an interface, it's useful to be able to pretend that an object has a certain attribute people can read from and write to, while in reality there's more going on behind the screen: a certain calculation, creating an object, doing a lookup in different places and so on — in essence, properties that work like functions, or functions that look like properties. This is a very useful abstraction that makes code more domain-specific. | ||
|
||
Unfortunately, as opposed to Ruby and Python, Javascript has no support for making a function appear to be a property. Instead, you'll see the following idiom appear across different Javascript libraries: | ||
|
||
.. literalinclude:: ../../../examples/getter-setters.jsx | ||
|
||
Extendables makes considerable use of this idiom, which you're probably familiar with if you've ever used `jQuery <http://jquery.com/>`_ before. | ||
|
||
domain-specific language constructs | ||
=================================== | ||
|
||
* esp. in the UI interface | ||
* make code read more like plain English, at the expense of having function names that are, perhaps, in themselves not always very descriptive (e.g. ``obj.is`` in itself might not make a lot of sense, even though it's pretty obvious what ``obj.is(Array)`` does.) | ||
Some parts of Extendables try to make coding a bit more elegant and a bit more like plain English by introducing a `domain-specific language <http://en.wikipedia.org/wiki/Domain-specific_language>`_ within Javascript. | ||
|
||
For example, in the UI module you would add styles to a dialog using ``var dialog = ui.Dialog("Hello there").with(styles);`` and not ``var dialog = ui.Dialog("Hello there"); dialog.add_styles(styles);`` | ||
|
||
In the Jasmine testing library, you define tests that look like | ||
|
||
.. code-block:: javascript | ||
it('needs proper unit tests', function () { | ||
expect(false).toBeTruthy(); | ||
}); | ||
By themselves, the function names ``it`` and ``expect`` are vague and undescriptive. But in context, they lead to very elegant code that makes immediate sense. Extendables uses domain-specific constructs wherever a single isolated task needs to be accomplished (building a user interface, testing your code) but avoids them elsewhere, in favor of more generally descriptive function and class names. | ||
|
||
In Extendables, modules that implement a domain-specific constructs are referred to as frameworks, because they frame your entire way of coding, whereas a library includes mostly helper classes and functions. | ||
|
||
extending prototypes | ||
==================== | ||
|
||
* people should make sure not to override these methods | ||
Extendables does most of its work by extending the prototypes of built-in objects, like ``Number``, ``Array`` and ``String``. That way, we can keep our code entirely object-oriented instead of having | ||
to define a plethora of helper functions. | ||
|
||
If a developer wishes to extend object prototypes him- or herself, they need to be aware of which methods Extendables adds to the prototypes. Overriding prototype methods provided by Extendables could unpredictably alter how applications work, so some care needs to be taken. | ||
|
||
.. | ||
- factories (Error handling) | ||
- basic functional programming using select/reject/compact in Extendables' internal code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ Core | |
|
||
writing-a-module | ||
contribute | ||
design-patterns | ||
roadmap | ||
changes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function Person () { | ||
this.is_child = false; | ||
|
||
// private attribute | ||
this._age = null; | ||
|
||
this.age = function (value) { | ||
if (value) { | ||
// setter | ||
this._age = value; | ||
if (this._age < 18) { | ||
this.is_child = true; | ||
} | ||
} else { | ||
// getter | ||
if (this._age instanceof Number) { | ||
return "{_age} years old".format(this); | ||
} else { | ||
return this._age; | ||
} | ||
} | ||
} | ||
} | ||
|
||
var timmy = new Person(); | ||
// this sets the person's age | ||
timmy.age(13); | ||
// this returns the person's age | ||
$.writeln(timmy.age()); | ||
// this allows our object to do more than just setting an attribute | ||
$.writeln(timmy.is_child == true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
describe('Example code', function () { | ||
var examples = new Folder("examples").at(Folder.extendables); | ||
|
||
it('has a working basic code sample', function () { | ||
$.evalFile(new File("basic.jsx").at(examples)); | ||
}); | ||
it('has a working code sample showcasing array functionality', function () { | ||
$.evalFile(new File("arrays.jsx").at(examples)); | ||
}); | ||
it('has a working code sample showcasing methods on File and Folder', function () { | ||
$.evalFile(new File("files-and-folders.jsx").at(examples)); | ||
}); | ||
it('has a working code sample showing the getter/setter design pattern', function () { | ||
$.evalFile(new File("getter-setters.jsx").at(examples)); | ||
}); | ||
it('has a working code sample that constructs a user interface', function () { | ||
$.evalFile(new File("ui.jsx").at(examples)); | ||
}); | ||
it('has a working code sample that does XML traversal', function () { | ||
$.evalFile(new File("xml-traversal.jsx").at(examples)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters