Skip to content
This repository has been archived by the owner on Nov 30, 2018. It is now read-only.

Commit

Permalink
Adding url subscriber. Closes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Jun 5, 2013
1 parent 77fb823 commit f99e55d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ Generates:
<legend type="ui-dform-legend">Address</label>
</fieldset>

**url** *{String|Object}*<br />
The `url` subscriber issues a `$(element).dform('ajax', options)` request to load content from remote files.

{
"type" : "div",
"url": "form.json"
}

**type** *{String}*<br />
Besides looking up the correct Type Generator it also adds a dform specific class to the element using
`$.dform.options.prefix` (*ui-dform-* by default) and the type name.
Expand Down
5 changes: 2 additions & 3 deletions src/dform.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,9 @@
* @param options
* @param type
*/
"url" : function (options, type) {
// TODO this.buildForm(options);
"url" : function (options) {
this.dform('ajax', options);
},

/**
* Post processing function, that will run whenever all other subscribers are finished.
*
Expand Down
15 changes: 8 additions & 7 deletions src/dform.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* @return {Object} A new object with all properties of the given object, except
* for the ones given in the list of keys
*/
withoutKeys = function (object, keys) {
withoutKeys = function (object, keys) {
var result = {};
each(object, function (index, value) {
if (!~$.inArray(index, keys)) {
Expand Down Expand Up @@ -278,18 +278,19 @@
*/
ajax : function (params, success, error) {
var options = {
success: function (data) {
self.dform(data);
if(success) {
success.call(self, data);
}
},
error : error,
url : params
}, self = this;
if (typeof params !== 'string') {
$.extend(options, params);
}
options.success = function (data) {
var callback = success || params.success;
self.dform(data);
if(callback) {
callback.call(self, data);
}
}
$.ajax(options);
},
/**
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ $(document).ready(function () {
equal(created.data('test'), 'Test data attribute', 'Added test data attribute');
});

test("Ajax", 2, function() {
test("Ajax", 3, function() {
stop();
$('<div>').dform('test.json', function(data) {
equal(data.html, 'The test', 'Data passed to success callback');
Expand Down
26 changes: 20 additions & 6 deletions test/test_subscribers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ $(document).ready(function () {

test("caption", function () {
var simple = $('<div>').dform({
type : 'text',
caption : 'The test'
}).find('label'),
type : 'text',
caption : 'The test'
}).find('label'),
asElement = $('<div>').dform({
type : 'text',
caption : {
Expand All @@ -89,9 +89,9 @@ $(document).ready(function () {

test("options", function () {
var options = {
test1 : 'Test 1',
test2 : 'Test 2'
},
test1 : 'Test 1',
test2 : 'Test 2'
},
select = $('<select>').dform({
type : 'select',
options : $.extend(true, { test3 : {
Expand All @@ -109,4 +109,18 @@ $(document).ready(function () {
ok(checkboxes.find('[type="checkbox"][value="test1"]').length);
equal(checkboxes.find('label:first').html(), 'Test 1', 'Set for and found label');
});

test("url", function() {
stop();
$('<div>').dform({
type: 'div',
url: {
url: 'test.json',
success: function() {
equal($(this).html(), 'The test', 'Subscriber ran, got text');
start();
}
}
});
});
});

0 comments on commit f99e55d

Please sign in to comment.