|
8 | 8 | var _subscriptions = {},
|
9 | 9 | _types = {},
|
10 | 10 | each = $.each,
|
11 |
| - addToObject = function(obj) { |
| 11 | + addToObject = function (obj) { |
12 | 12 | 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) { |
15 | 15 | result(name, val, condition);
|
16 | 16 | });
|
17 |
| - } else if(condition === undefined || condition === true) { |
18 |
| - if(!obj[data]) { |
| 17 | + } else if (condition === undefined || condition === true) { |
| 18 | + if (!obj[data]) { |
19 | 19 | obj[data] = [];
|
20 | 20 | }
|
21 | 21 | obj[data].push(fn);
|
|
30 | 30 | * @param {Object} object The object to use
|
31 | 31 | * @return {Array} An array containing all properties in the object
|
32 | 32 | */
|
33 |
| - keyset = function (object) { |
| 33 | + keyset = function (object) { |
34 | 34 | return $.map(object, function (val, key) {
|
35 | 35 | return key;
|
36 | 36 | });
|
|
44 | 44 | * @return {Object} A new object containing only the properties
|
45 | 45 | * with names given in keys
|
46 | 46 | */
|
47 |
| - withKeys = function (object, keys) { |
| 47 | + withKeys = function (object, keys) { |
48 | 48 | var result = {};
|
49 | 49 | each(keys, function (index, value) {
|
50 | 50 | if (object[value]) {
|
|
62 | 62 | * @return {Object} A new object with all properties of the given object, except
|
63 | 63 | * for the ones given in the list of keys
|
64 | 64 | */
|
65 |
| - withoutKeys = function (object, keys) { |
| 65 | + withoutKeys = function (object, keys) { |
66 | 66 | var result = {};
|
67 | 67 | each(object, function (index, value) {
|
68 | 68 | if (!~$.inArray(index, keys)) {
|
|
80 | 80 | * @param {String} type The type of the current element as in the registered types
|
81 | 81 | * @return {Object} The jQuery object
|
82 | 82 | */
|
83 |
| - runSubscription = function (name, options, type) { |
| 83 | + runSubscription = function (name, options, type) { |
84 | 84 | if ($.dform.hasSubscription(name)) {
|
85 | 85 | this.each(function () {
|
86 | 86 | var element = $(this);
|
|
98 | 98 | * @param {Object} options The options to use
|
99 | 99 | * @return {Object} The jQuery element this function has been called on
|
100 | 100 | */
|
101 |
| - runAll = function (options) { |
| 101 | + runAll = function (options) { |
102 | 102 | var type = options.type, self = this;
|
103 | 103 | // Run preprocessing subscribers
|
104 | 104 | this.dform('run', '[pre]', options, type);
|
|
171 | 171 | * @return {Object} Mapping from subscriber names
|
172 | 172 | * to an array of subscriber functions.
|
173 | 173 | */
|
174 |
| - subscribers : function(name) { |
| 174 | + subscribers : function (name) { |
175 | 175 | return name ? _subscriptions[name] : _subscriptions;
|
176 | 176 | },
|
177 | 177 | /**
|
|
189 | 189 | *
|
190 | 190 | * @param {String} name The subscriber name
|
191 | 191 | * @return {Boolean} True if the given name has at least one subscriber registered,
|
192 |
| - * false otherwise |
| 192 | + * false otherwise |
193 | 193 | */
|
194 | 194 | hasSubscription : function (name) {
|
195 | 195 | return _subscriptions[name] ? true : false;
|
|
199 | 199 | *
|
200 | 200 | * @param {Object} options - The options to use
|
201 | 201 | * @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. |
203 | 203 | */
|
204 | 204 | createElement : function (options) {
|
205 | 205 | if (!options.type) {
|
206 | 206 | throw "No element type given! Must always exist.";
|
207 | 207 | }
|
208 | 208 | var type = options.type,
|
209 | 209 | element = null,
|
210 |
| - // We don't need the type key in the options |
| 210 | + // We don't need the type key in the options |
211 | 211 | opts = $.withoutKeys(options, ["type"]);
|
212 | 212 |
|
213 | 213 | if (_types[type]) {
|
|
286 | 286 | }
|
287 | 287 | options.success = function (data) {
|
288 | 288 | self.dform(data);
|
289 |
| - success(data); |
| 289 | + if(success) { |
| 290 | + success(data, self); |
| 291 | + } |
290 | 292 | }
|
291 | 293 | $.ajax(options);
|
292 | 294 | },
|
|
318 | 320 | * @param {String} converter The name of the converter in $.dform.converters
|
319 | 321 | * that will be used to convert the options
|
320 | 322 | */
|
321 |
| - $.fn.dform = function (options, converter) { |
| 323 | + $.fn.dform = function (options, converter, error) { |
322 | 324 | var self = $(this);
|
323 | 325 | if ($.dform.methods[options]) {
|
324 | 326 | $.dform.methods[options].apply(self, Array.prototype.slice.call(arguments, 1));
|
325 | 327 | } 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 | + } |
327 | 336 | }
|
328 | 337 | return this;
|
329 | 338 | }
|
|
0 commit comments