Skip to content

Commit 7ec250e

Browse files
author
daffl
committed
Documentation and test updates
1 parent b283a48 commit 7ec250e

File tree

5 files changed

+182
-128
lines changed

5 files changed

+182
-128
lines changed

readme.md

Lines changed: 122 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,133 @@
1-
The jQuery.dForm plugin allows you to create your HTML forms programmatically as JavaScript objects or
2-
as [JSON](http://json.org). Let the browser do the work and generate JavaScript enhanced forms without hassle.
1+
The jQuery.dForm plugin generates HTML markup from JavaScript objects and [JSON](http://json.org)
2+
with a focus on HTML forms.
33

4-
### You should try this plugin if you want to
4+
__Some things you can do:__
55

6-
* write JavaScript instead of HTML markup since your page doesn't run without JS anyway
7-
* manage all your form related jQuery plugins in a unified way
8-
* have an easy way to include jQuery UI elements and JavaScript validation
6+
* use JavaScript and JSON instead of HTML markup since your page doesn't run without JS anyway
7+
* generate JavaScript enhanced markup with your own extensions and custom types
8+
* have an easy way to include jQuery UI elements and JavaScript validation (both supported out of the box)
99
* scaffold forms from business objects of your server side framework
1010

1111
## Get started:
1212

13-
All examples are hosted on [JSFiddle](http://jsfiddle.net). Feel free to play around, fork and comment:
14-
15-
<iframe style="width: 90%; height: 450px" src="http://jsfiddle.net/Daff/yREkc/embedded/js,html,result"
16-
allowfullscreen="allowfullscreen" frameborder="0">
17-
</iframe>
18-
19-
## How to get it:
20-
21-
[Download](http://github.com/downloads/daffl/jquery.dform/jquery.dform-0.1.4.tar.gz) the latest package (0.1.4(
22-
23-
Clone the current master on [GitHub](http://github.com/daffl/jquery.dform/)
24-
25-
26-
## How to get involved:
27-
13+
[Download](http://github.com/downloads/daffl/jquery.dform/jquery.dform-0.1.4.tar.gz)
14+
the latest version 0.2.0 (6 Kb minified)
15+
16+
Then try this JavaScript snipped:
17+
18+
$("#myform").dform({
19+
"action" : "index.html",
20+
"method" : "get",
21+
"html" :
22+
[
23+
{
24+
"type" : "p",
25+
"html" : "You must login"
26+
},
27+
{
28+
"name" : "username",
29+
"id" : "txt-username",
30+
"caption" : "Username",
31+
"type" : "text",
32+
"placeholder" : "E.g. user@example.com"
33+
},
34+
{
35+
"name" : "password",
36+
"caption" : "Password",
37+
"type" : "password"
38+
},
39+
{
40+
"type" : "submit",
41+
"value" : "Login"
42+
}
43+
]
44+
});
45+
46+
__Learn more:__
47+
48+
* Check out the JSFiddle examples or the get started page for more examples
2849
* Visit the [jQuery.dForm Google Group](http://groups.google.com/group/jquery-dform)
29-
* Fork the project on [GitHub](http://github.com/daffl/jZquery.dform/)
50+
* Fork the project on [GitHub](http://github.com/daffl/jquery.dform/)
3051
* Follow [@daffl](http://twitter.com/daffl) on Twitter
31-
32-
33-
34-
35-
# Types
36-
37-
# Subscribers
38-
39-
* Subscribers are the core concept of the jQuery.dForm.
40-
*
41-
* They are functions, that will be called when traversing the options
42-
* passed to the plugin.
43-
* You can use the already existing subscribers as well as register your own.
44-
*
45-
* The plugin has three different types of subscribers
46-
*
47-
* Types - For creating a new element of the given type.
48-
* Subscribers - Which will be called when the name they are registered with
49-
* appears in the options given to the plugin and after the type element has been added to the DOM.
50-
* Special subscribers - Will be called on special events.
51-
*
52-
* Currently there are two types of special subscribers
53-
* * [pre] - Functions registered with this name will be called before any processing occurs.
54-
* * [post] - Functions registered with this name will be called after all processing is finished.
55-
*
56-
* Example:
57-
* (start code)
58-
* $("#myform").buildForm({
59-
* "name" : "textfield",
60-
* "type" : "text",
61-
* "id" : "testid",
62-
* "value" : "Testvalue",
63-
* "caption" : "Label for textfield"
64-
* });
65-
* (end)
66-
*
67-
* The above JavaScript snippet will do the following
68-
*
69-
* - Look for a type subscriber called <text>, which creates an input field with the type text or, if
70-
* there is no matching type call the <defaultType> function which creates a HTML tag of the given type.
71-
* - Add all attributes as HTML attributes to the input element that don't have a
72-
* subscriber registered (which are name and id)
73-
* - Add the new element to the DOM (append to #myform).
74-
* - Run the <type> subscriber which adds the auto generated class name ui-dform-text to the input field
75-
* - Run the <value> subscriber which sets the value of this form element
76-
* - Run the <caption> subscriber which adds a label before the textfield
77-
*
78-
* Read in the <Extensions> chapter, how you can extend the dForm Plugin with your own
79-
* types and subscribers.
80-
*
81-
* This page will list the basic <Types> and <Subscribers> that are
82-
* supported by the plugin as well as examples for using them.
52+
* Read on in this documentation
53+
54+
## Types
55+
56+
Type Generators are functions that return a new jQuery DOM object for a specific type. If there is no Type Generator
57+
with a given name, a basic HTML element with that tag name will be created. Every other key in the JavaScript or JSON object
58+
you pass will be used as an HTML attribute. An exception is, if there is a Subscriber registered for that ke (more about
59+
this in the next section). A plugin call like this:
60+
61+
$('#my-div').dform({
62+
type : "span",
63+
id : "the-span"
64+
});
65+
66+
Will append an empty \<span id="the-span"\> tag to the element with the id my-div.
67+
Besides standard HTML tags the following core types are supported:
68+
69+
* __container__: Creates a \<div\> container (you can also use { type : 'div' })
70+
* __text__: Creates a text input field
71+
* __password__: Creates a password input field
72+
* __submit__: Creates a submit button input element
73+
* __reset__: Creates a reset button input element
74+
* __hidden__: Creates a hidden input element
75+
* __file__: Create a file upload field
76+
* __radio__: Creates a radio button
77+
* __checkbox__: Creates a checkbox
78+
* __radiobuttons__: Creates a group of radiobuttons (uses options subscriber explained below)
79+
* __checkboxes__: Creates a group of checkboxes
80+
* __number__: Creates an HTML 5 number input field
81+
* __url__: Creates an HTML 5 url input field
82+
* __tel__: Creates an HTML 5 phone number input field
83+
* __email__: Creates an HTML 5 email input field
84+
85+
## Subscribers
86+
87+
Not everything can be solved using a custom type. Adding a class for example doesn't need to be implemented
88+
every time and this is where Subscribers come in. Subscribers are functions that will be called for the key they
89+
have been registered for when traversing the object that has been passed to the plugin.
90+
91+
### Core subscribers
92+
93+
* class:
94+
* html:
95+
* elements:
96+
* value:
97+
* css:
98+
* options:
99+
* caption:
100+
* url:
101+
* type:
102+
103+
### Special subscribers
104+
105+
Currently there are two types of special subscribers
106+
107+
* __\[pre\]__ Functions registered with this name will be called before any processing occurs.
108+
* __\[post\]__ Functions registered with this name will be called after all processing is finished.
109+
110+
111+
### Processing example:
112+
113+
Here is a quick example walkthrough:
114+
115+
$("#myform").dform({
116+
"name" : "textfield",
117+
"type" : "text",
118+
"id" : "testid",
119+
"value" : "Testvalue",
120+
"caption" : "Label for textfield"
121+
});
122+
123+
Which will do the following:
124+
125+
* Look for a Type Generator with the name __text__, which creates an input field with the type text
126+
* Add the HTML attributes to the input element that don't have a subscriber registered (which are __name__ and __id__)
127+
* Add the new element to the DOM (append to #myform)
128+
* Run the __type__ subscriber which adds the auto generated class name _ui-dform-text_ to the input field
129+
* Run the __value__ subscriber which sets the value of this form element
130+
* Run the __caption__ subscriber which adds a label before the textfield
83131

84132
# Plugin support
85133

@@ -91,7 +139,6 @@ Clone the current master on [GitHub](http://github.com/daffl/jquery.dform/)
91139

92140
## jQuery Globalize
93141

94-
95142
# Changelog
96143

97144
### 0.2.0

src/dform.extensions.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
* type - The jQuery UI type
8383
* options - The options to evaluate
8484
*/
85-
function _getOptions(type, options)
85+
var getOptions = function(type, options)
8686
{
8787
var keys = $.keyset($.ui[type]["prototype"]["options"]);
8888
return $.withKeys(options, keys);
@@ -109,7 +109,7 @@
109109
$.dform.addTypeIf($.isFunction($.fn.progressbar), "progressbar",
110110
function(options)
111111
{
112-
return $("<div>").dformAttr(options).progressbar(_getOptions("progressbar", options));
112+
return $("<div>").dform('attr', options).progressbar(_getOptions("progressbar", options));
113113
});
114114

115115
/**
@@ -134,7 +134,7 @@
134134
$.dform.addTypeIf($.isFunction($.fn.slider), "slider",
135135
function(options)
136136
{
137-
return $("<div>").dformAttr(options).slider(_getOptions("slider", options));
137+
return $("<div>").dform('attr', options).slider(_getOptions("slider", options));
138138
});
139139

140140
/**
@@ -180,7 +180,7 @@
180180
$.dform.addTypeIf($.isFunction($.fn.accordion), "accordion",
181181
function(options)
182182
{
183-
return $("<div>").dformAttr(options);
183+
return $("<div>").dform('attr', options);
184184
});
185185

186186
/**
@@ -232,7 +232,7 @@
232232
$.dform.addTypeIf($.isFunction($.fn.tabs),
233233
"tabs", function(options)
234234
{
235-
return $("<div>").dformAttr(options);
235+
return $("<div>").dform('attr', options);
236236
});
237237

238238
/**
@@ -274,7 +274,7 @@
274274
$.each(options, function(index, options) {
275275
var id = options.id ? options.id : index;
276276
$.extend(options, { "type" : "container", "id" : id });
277-
$(scoper).formElement(options);
277+
$(scoper).dform('append', options);
278278
var label = $(scoper).children("div:last").prev();
279279
$(label).wrapInner($("<a>").attr("href", "#" + id));
280280
$(ul).append($("<li>").wrapInner(label));
@@ -573,7 +573,7 @@
573573
$.dform.subscribeIf($.isFunction($.fn.ajaxForm), "ajax",
574574
function(options, type)
575575
{
576-
if(type == "form")
576+
if(type === "form")
577577
{
578578
this.ajaxForm(options);
579579
}
@@ -688,7 +688,7 @@
688688
$(this).html('');
689689
var optlist = _getTranslate(options);
690690
if(optlist) {
691-
$(this).runSubscription("options", optlist, type);
691+
$(this).dform('run', 'options', optlist, type);
692692
}
693693
}
694694
});

0 commit comments

Comments
 (0)