Skip to content

Commit cfdd92c

Browse files
author
daffl
committed
Ajax works and more docs
1 parent e689045 commit cfdd92c

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

readme.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Now generates
161161
## Subscribers
162162

163163
While type generators are being used to generate a base element for the given type, subscribers attach to
164-
certain attributes in the options object. When traversing the object all subscribers registered for the
164+
certain attributes in the options object. When traversing the object, all subscribers registered for the
165165
current key will be executed on the current element.
166166

167167
### Core subscribers
@@ -478,24 +478,29 @@ to an array of generator functions will be returned.
478478

479479
**$.dform.addType(name, generator \[, condition\])** *{String}* *{Function}* *{Boolean}*<br />
480480
Add a new type with a given name and generator function which takes the options as the parameter
481-
and returns a new element. Optionally pass a condition which will add the type only if
482-
it returns true.
481+
and returns a new element. Optionally pass a condition which will add the type only if it is true.
483482

484-
**$.dform.subscribe(name, subscriber \[, condition\])**<br />
485-
**$.dform.subscribers**<br />
483+
**$.dform.subscribe(name, subscriber \[, condition\])** *{String}* *{Function}* *{Boolean}*<br />
484+
Add a new subscriber function for a given name that takes the value and type name as the parameter and will have
485+
`this` set to the current element. Optionally pass as condition which will add the subscriber only if it is true.
486+
487+
**$.dform.subscribers(\[name\])**<br />
488+
Returns all subscribers for a given name. If no name is given, an object containing all subscribers will
489+
be returned.
486490

487491
**$.dform.hasSubscription(name)** *{String}*<br />
488-
Returns if there is any subscribers with the given name.
492+
Returns if there is at least one subscriber registered with the given name.
489493

490-
**$.dform.createElement(options)**<br />
494+
**$.dform.createElement(options)** *{Object}*<br />
495+
Returns a new element either using a registered type generator or the default type generator.
491496

492497
## jQuery UI
493498

494499
jQuery.dForm automatically adds support for whichever jQuery UI plugin is available.
495500
If the form has the *ui-widget* class the plugin will automatically turn buttons into jQuery UI buttons and add
496501
corners to *text*, *textarea*, *password* and *fieldset* elements.
497502

498-
> Note: jQuery UI has to be loaded __before__ the plugin.
503+
> Note: jQuery UI has to be loaded __before__ the dForm plugin.
499504
500505
### Types
501506

@@ -563,8 +568,7 @@ to the element:
563568
}
564569
}
565570

566-
If the form has the *ui-widget* class the jQuery UI CSS error classes will be used to highlight
567-
invalid fields.
571+
If the form has the *ui-widget* class the jQuery UI CSS error classes will be used to highlight fields.
568572

569573
### jQuery Globalize
570574

@@ -650,4 +654,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
650654
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
651655
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
652656
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
653-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
657+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

src/dform.js

+26-17
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
var _subscriptions = {},
99
_types = {},
1010
each = $.each,
11-
addToObject = function(obj) {
11+
addToObject = function (obj) {
1212
var result = function (data, fn, condition) {
13-
if(typeof data === 'object') {
14-
$.each(data, function(name, val) {
13+
if (typeof data === 'object') {
14+
$.each(data, function (name, val) {
1515
result(name, val, condition);
1616
});
17-
} else if(condition === undefined || condition === true) {
18-
if(!obj[data]) {
17+
} else if (condition === undefined || condition === true) {
18+
if (!obj[data]) {
1919
obj[data] = [];
2020
}
2121
obj[data].push(fn);
@@ -30,7 +30,7 @@
3030
* @param {Object} object The object to use
3131
* @return {Array} An array containing all properties in the object
3232
*/
33-
keyset = function (object) {
33+
keyset = function (object) {
3434
return $.map(object, function (val, key) {
3535
return key;
3636
});
@@ -44,7 +44,7 @@
4444
* @return {Object} A new object containing only the properties
4545
* with names given in keys
4646
*/
47-
withKeys = function (object, keys) {
47+
withKeys = function (object, keys) {
4848
var result = {};
4949
each(keys, function (index, value) {
5050
if (object[value]) {
@@ -62,7 +62,7 @@
6262
* @return {Object} A new object with all properties of the given object, except
6363
* for the ones given in the list of keys
6464
*/
65-
withoutKeys = function (object, keys) {
65+
withoutKeys = function (object, keys) {
6666
var result = {};
6767
each(object, function (index, value) {
6868
if (!~$.inArray(index, keys)) {
@@ -80,7 +80,7 @@
8080
* @param {String} type The type of the current element as in the registered types
8181
* @return {Object} The jQuery object
8282
*/
83-
runSubscription = function (name, options, type) {
83+
runSubscription = function (name, options, type) {
8484
if ($.dform.hasSubscription(name)) {
8585
this.each(function () {
8686
var element = $(this);
@@ -98,7 +98,7 @@
9898
* @param {Object} options The options to use
9999
* @return {Object} The jQuery element this function has been called on
100100
*/
101-
runAll = function (options) {
101+
runAll = function (options) {
102102
var type = options.type, self = this;
103103
// Run preprocessing subscribers
104104
this.dform('run', '[pre]', options, type);
@@ -171,7 +171,7 @@
171171
* @return {Object} Mapping from subscriber names
172172
* to an array of subscriber functions.
173173
*/
174-
subscribers : function(name) {
174+
subscribers : function (name) {
175175
return name ? _subscriptions[name] : _subscriptions;
176176
},
177177
/**
@@ -189,7 +189,7 @@
189189
*
190190
* @param {String} name The subscriber name
191191
* @return {Boolean} True if the given name has at least one subscriber registered,
192-
* false otherwise
192+
* false otherwise
193193
*/
194194
hasSubscription : function (name) {
195195
return _subscriptions[name] ? true : false;
@@ -199,15 +199,15 @@
199199
*
200200
* @param {Object} options - The options to use
201201
* @return {Object} The element as created by the builder function specified
202-
* or returned by the defaultType function.
202+
* or returned by the defaultType function.
203203
*/
204204
createElement : function (options) {
205205
if (!options.type) {
206206
throw "No element type given! Must always exist.";
207207
}
208208
var type = options.type,
209209
element = null,
210-
// We don't need the type key in the options
210+
// We don't need the type key in the options
211211
opts = $.withoutKeys(options, ["type"]);
212212

213213
if (_types[type]) {
@@ -286,7 +286,9 @@
286286
}
287287
options.success = function (data) {
288288
self.dform(data);
289-
success(data);
289+
if(success) {
290+
success(data, self);
291+
}
290292
}
291293
$.ajax(options);
292294
},
@@ -318,12 +320,19 @@
318320
* @param {String} converter The name of the converter in $.dform.converters
319321
* that will be used to convert the options
320322
*/
321-
$.fn.dform = function (options, converter) {
323+
$.fn.dform = function (options, converter, error) {
322324
var self = $(this);
323325
if ($.dform.methods[options]) {
324326
$.dform.methods[options].apply(self, Array.prototype.slice.call(arguments, 1));
325327
} else {
326-
$.dform.methods.init.apply(self, arguments);
328+
if (typeof options === 'string') {
329+
$.dform.methods.ajax.call(self, {
330+
url : options,
331+
dataType : 'json'
332+
}, converter, error);
333+
} else {
334+
$.dform.methods.init.apply(self, arguments);
335+
}
327336
}
328337
return this;
329338
}

test/test.js

+9
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@ $(document).ready(function () {
5656
equal(created.attr('id'), 'test-id', 'ID added');
5757
equal(created.data('test'), 'Test data attribute', 'Added test data attribute');
5858
});
59+
60+
test("Ajax", function() {
61+
stop();
62+
$('<div>').dform('test.json', function(data, form) {
63+
equal(data.html, 'The test', 'Data passed to success callback');
64+
equal(form.html(), 'The test', 'Form got created');
65+
start();
66+
});
67+
})
5968
});

test/test.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "div",
3+
"html" : "The test"
4+
}

0 commit comments

Comments
 (0)