Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding documentation

  • Loading branch information...
commit 679b3dd8bd7d5a3ddfbcd97aeaea916ac63a820b 1 parent 385bb1c
Hamish Friedlander authored August 13, 2009

Showing 2 changed files with 277 additions and 0 deletions. Show diff stats Hide diff stats

  1. 10  LICENSE
  2. 267  README.textile
10  LICENSE
... ...
@@ -0,0 +1,10 @@
  1
+Copyright (C) 2009 Hamish Friedlander (hamish@silverstripe.com) and SilverStripe Limited (www.silverstripe.com)
  2
+All rights reserved.
  3
+
  4
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5
+
  6
+    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7
+    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8
+    * Neither the name of Hamish Friedlander nor SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9
+
  10
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267  README.textile
Source Rendered
... ...
@@ -0,0 +1,267 @@
  1
+h1. Concrete - Support for ConcreteUI programming in jQuery
  2
+
  3
+A basic desire for jQuery programming is some sort of OO or other organisational method for code. For your consideration,
  4
+we provide a library for ConcreteUI style programming. In ConcreteUI you attach behavioral code to DOM objects. Concrete extends this
  5
+concept beyond what is provided by other libraries to provide a very easy to use system with class like, ploymorphic, namespaced properties
  6
+
  7
+h2. Basic use
  8
+
  9
+h4. First intro
  10
+
  11
+To attach methods to DOM nodes, call the `concrete` function on a jQuery selector object, passing a hash listing the method names and bodys
  12
+
  13
+<pre><code>
  14
+  $('div').concrete({
  15
+    foo: function(..){..},
  16
+    bar: function(..){..}
  17
+  });
  18
+</code></pre>
  19
+
  20
+You can then call those methods on any jQuery object.
  21
+
  22
+<pre><code>
  23
+  $('#a').foo();
  24
+</code></pre>
  25
+
  26
+Any elements in the jQuery selection that match the selector used during definition ('div' in this example) will have foo called with that element
  27
+set as this. Any other objects are skipped. The return value will be the return value of foo() for the last matched DOM object in the set
  28
+
  29
+h4. A proper example
  30
+
  31
+Given this DOM structure:
  32
+
  33
+<pre><code>
  34
+  <body>
  35
+    <div class="internal_text">Internal text</div>
  36
+    <div class="attribute_text" rel="Attribute text"></div>
  37
+    <div>Nonsense</div>
  38
+  </body>
  39
+</code></pre>
  40
+
  41
+And this concrete definition
  42
+
  43
+<pre><code>
  44
+  $('.internal_text').concrete({
  45
+    foo: function(){ console.log(this.text()); }
  46
+  });
  47
+  $('.attribute_text').concrete({
  48
+    foo: function(){ console.log(this.attr('rel')); }
  49
+  });
  50
+</code></pre>
  51
+  
  52
+Then this call
  53
+
  54
+<pre><code>
  55
+  $('div').foo();
  56
+</code></pre>
  57
+
  58
+Will log this to the console
  59
+
  60
+<pre><code>
  61
+  Internal text
  62
+  Attribute text  
  63
+</code></pre>
  64
+
  65
+h4. Limitations
  66
+
  67
+When defining methods, the jQuery object that concrete is called on must be a plain selector, without context. These examples will not work
  68
+
  69
+<pre><code>
  70
+  $('div', el).concrete(...)
  71
+  $([ela, elb, elc]).concrete(...)
  72
+  $('<div id="a"></div>').concrete(...)
  73
+</code></pre>
  74
+
  75
+h2. Live
  76
+
  77
+The definitions you provide are not bound to the elements that match at definition time. You can declare behaviour prior to the DOM existing in any
  78
+form (i.e. prior to DOMReady) and later calls will function correctly.
  79
+
  80
+h2. Selector specifity
  81
+
  82
+When there are two definitions for a particular method on a particular DOM node, the function with the most _specific_ selector is used. 
  83
+_Specifity_ is calculated as defined by the CSS 2/3 spec. This can be seen as _subclassing_ applied to behaviour.
  84
+
  85
+Another example. Given this DOM structure
  86
+
  87
+<pre><code>
  88
+  <body>
  89
+    <div>Internal text</div>
  90
+    <div class="attribute_text" rel="Attribute text"></div>
  91
+    <div>Nonsense</div>
  92
+  </body>
  93
+</code></pre>
  94
+
  95
+And this concrete definition
  96
+
  97
+<pre><code>
  98
+  $('div').concrete({
  99
+    foo: function(){ console.log(this.text()); }
  100
+  });
  101
+  $('.attribute_text').concrete({
  102
+    foo: function(){ console.log(this.attr('rel')); }
  103
+  });
  104
+</code></pre>
  105
+
  106
+Then this call
  107
+
  108
+<pre><code>
  109
+  $('div').foo();
  110
+</code></pre>
  111
+
  112
+Will log this to the console
  113
+
  114
+<pre><code>
  115
+  Internal text
  116
+  Attribute text
  117
+  Nonsense
  118
+</code></pre>
  119
+
  120
+h2. Events
  121
+ 
  122
+If you declare a function with a name starting with 'on', then instead of defining that function, it will be bound to an event of that
  123
+name. Just like other functions this binding will be live, and only the most specific definition will be used
  124
+
  125
+<pre><code>
  126
+  <head>
  127
+    <script type='text/javascript'>
  128
+      /* No need for onready wrapper. Events are bound as needed */
  129
+      $('div').concrete({
  130
+        onclick: function(){ this.css({backgroundColor: 'blue'}); }
  131
+      });
  132
+      $('.green').concrete({
  133
+        onclick: function(){ this.css({color: 'green'}); }
  134
+      });
  135
+    </script>
  136
+  <body>
  137
+    <div>Background will turn blue when clicked on</div>
  138
+    <div>Will also have blue background when clicked on</div>
  139
+    <div class='green'>Will have green text when clicked on. Background color will not change</div>
  140
+  </body>
  141
+</code></pre>
  142
+  
  143
+h2. Constructors / Destructors
  144
+
  145
+Declaring a function with the name `onmatch` will create a behavior that is called on each object when it matches. Likewise, `onunmatch` will
  146
+be called when an object that did match this selector stops matching it (because it is removed, or because you've changed its properties).
  147
+
  148
+Note that an onunmatch block must be paired with an onmatch block - an onunmatch without an onmatch _in the same concrete definition block_ is illegal
  149
+
  150
+Like other functions, only the most specific definition will be used. However, because property changes are not atomic, this may not work as you
  151
+expect.
  152
+
  153
+h2. Namespaces
  154
+
  155
+To avoid name clashes, to allow multiple bindings to the same event, and to generally seperate a set of functions from other code you can use namespaces
  156
+
  157
+<pre><code>
  158
+  $('div').concrete('foo.bar', function($){ return {
  159
+      baz: function(){}
  160
+  }});
  161
+</code></pre>
  162
+  
  163
+You can then call these functions like this:
  164
+
  165
+<pre><code>
  166
+  $('div').concrete('foo.bar').baz()
  167
+</code></pre>
  168
+  
  169
+Namespaced functions work just like regular functions (`this` is still set to a matching DOM Node). However, specifity is calculated per namespace.
  170
+This is particularly useful for events, because given this:
  171
+
  172
+<pre><code>
  173
+  $('div').concrete({
  174
+    onclick: function(){ this.css({backgroundColor: 'blue'}); }
  175
+  });
  176
+  
  177
+  $('div').concrete('foo', function($){ return {
  178
+      onclick: function(){ this.css({color: 'green'}); }
  179
+  }});
  180
+</code></pre>
  181
+  
  182
+Clicking on a div will change the background **and** foreground color.
  183
+
  184
+This is particularly important when writing reusable code, since otherwise you can't know before hand whether your event handler will be called or not
  185
+
  186
+Although a namespace can be any string, best practise is to name them with dotted-identifier notation.
  187
+
  188
+h4. Namespaces and scope (or What the hell's up with that ugly function closure)
  189
+
  190
+Inside a namespace definition, functions remember the namespace they are in, and calls to other functions will be looked up inside that namespace first. 
  191
+Where they don't exist, they will be looked up in the base namespace
  192
+
  193
+<pre><code>
  194
+  $('div').concrete('foo', {
  195
+    bar: function() { this.baz(); this.qux(); }
  196
+    baz: function() { console.log('baz'); }
  197
+  })
  198
+  
  199
+  $('div').concrete({
  200
+    qux: function() { console.log('qux'); }
  201
+  })
  202
+</code></pre>
  203
+  
  204
+Will print baz, qux to the console
  205
+
  206
+Note that 'exists' means that a function is declared in this namespace for _any_ selector, not just a matching one. Given the dom
  207
+
  208
+<pre><code>
  209
+  <div>Internal text</div>
  210
+</code></pre>
  211
+
  212
+And the concrete definitions
  213
+
  214
+<pre><code>
  215
+  $('div').concrete('foo', {
  216
+    bar: function() { this.baz(); }
  217
+  })
  218
+  
  219
+  $('span').concrete('foo' {
  220
+    baz: function() { console.log('a'); }
  221
+  
  222
+  $('div').concrete({
  223
+    baz: function() { console.log('b'); }
  224
+  })
  225
+</code></pre>
  226
+
  227
+Then doing $('div').bar(); will _not_ display b. Even though the span rule could never match a div, because baz is defined for some rule in the foo namespace, the base namespace will never be checked
  228
+
  229
+h4. Forcing base
  230
+
  231
+In some situations (such as the last example) you may want to force using the base namespace. In this case you can call concrete without any arguments. For example, if the first definition in the previous example was
  232
+
  233
+<pre><code>
  234
+  $('div').concrete('foo', {
  235
+    bar: function() { this.concrete().baz(); }
  236
+  })
  237
+</code></pre>
  238
+
  239
+Then b _would_ be output to the console. It is up to the developer to decide when they need to be explicit about using the base namespace
  240
+
  241
+h4. Using
  242
+
  243
+Sometimes a block outside of a namespace will need to refer to that namespace repeatedly. By passing a function to the concrete function, you can change the looked-up namespace
  244
+
  245
+<pre><code>
  246
+  $('div').concrete('foo', {
  247
+    bar: function() { console.log('a'); }
  248
+  })
  249
+  
  250
+  $('div').concrete('foo', function(){
  251
+    this.bar();
  252
+  });
  253
+</code></pre>
  254
+  
  255
+This equivilent to /with/ in javascript, and just like /with/, care should be taken to only use this construct in situations that merit it.
  256
+
  257
+
  258
+
  259
+
  260
+
  261
+
  262
+
  263
+
  264
+
  265
+ 
  266
+ 
  267
+ 

0 notes on commit 679b3dd

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