Skip to content

Commit 07b5cfa

Browse files
committed
Allow functions in AjaxURLS option
1 parent bdddff9 commit 07b5cfa

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,14 @@ var datasource = {
172172
var ajaxURLs = {
173173
'children': '/orgchart/children/',
174174
'parent': '/orgchart/parent/',
175-
'siblings': '/orgchart/siblings/',
176-
'families': '/orgchart/families/'
175+
// It would be helpful to have functions instead of URLs for the AJAX fetching
176+
// as it would allow a more flexible treatment of the results.
177+
'siblings': function(nodeData) {
178+
return '/orgchart/siblings/' + nodeData.id;
179+
},
180+
'families': function(nodeData) {
181+
return '/orgchart/families/' + nodeData.id;
182+
}
177183
};
178184

179185
$('#chart-container').orgchart({
@@ -533,7 +539,7 @@ $('#chartContainerId').orgchart(options);
533539
<td>toggleSiblingsResp</td><td>boolean</td><td>no</td><td>false</td><td>Once enable this option, users can show/hide left/right sibling nodes respectively by clicking left/right arrow.</td>
534540
</tr>
535541
<tr>
536-
<td>ajaxURL</td><td>json</td><td>no</td><td></td><td>It inclueds four properites -- parent, children, siblings, families(ask for parent node and siblings nodes). As their names imply, different propety indicates the URL to which ajax request for different nodes is sent.</td>
542+
<td>ajaxURL</td><td>json</td><td>no</td><td></td><td>It inclueds four properites -- parent, children, siblings, families(ask for parent node and siblings nodes). As their names imply, different propety provides the URL to which ajax request for different nodes is sent.</td>
537543
</tr>
538544
<tr>
539545
<td>depth</td><td>positive integer</td><td>no</td><td>999</td><td>It indicates the level that at the very beginning orgchart is expanded to.</td>

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "orgchart",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"homepage": "https://github.com/dabeng/OrgChart",
55
"authors": [
66
"dabeng <dabeng413@gmail.com>"

dist/js/jquery.orgchart.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@
641641
// start up loading status
642642
if (startLoading($that, $node, opts)) {
643643
// load new nodes
644-
$.ajax({ 'url': opts.ajaxURL.parent + nodeId + '/', 'dataType': 'json' })
644+
$.ajax({ 'url': $.isFunction(opts.ajaxURL.parent) ? opts.ajaxURL.parent(nodeData) : opts.ajaxURL.parent + nodeId, 'dataType': 'json' })
645645
.done(function(data) {
646646
if ($node.closest('.orgchart').data('inAjax')) {
647647
if (!$.isEmptyObject(data)) {
@@ -673,7 +673,7 @@
673673
} else { // load the new children nodes of the specified node by ajax request
674674
var nodeId = $that.parent()[0].id;
675675
if (startLoading($that, $node, opts)) {
676-
$.ajax({ 'url': opts.ajaxURL.children + nodeId + '/', 'dataType': 'json' })
676+
$.ajax({ 'url': $.isFunction(opts.ajaxURL.children) ? opts.ajaxURL.children(nodeData) : opts.ajaxURL.children + nodeId, 'dataType': 'json' })
677677
.done(function(data, textStatus, jqXHR) {
678678
if ($node.closest('.orgchart').data('inAjax')) {
679679
if (data.children.length) {
@@ -749,9 +749,11 @@
749749
} else {
750750
// load the new sibling nodes of the specified node by ajax request
751751
var nodeId = $that.parent()[0].id;
752-
var url = (getNodeState($node, 'parent').exist) ? opts.ajaxURL.siblings : opts.ajaxURL.families;
752+
var url = (getNodeState($node, 'parent').exist) ?
753+
($.isFunction(opts.ajaxURL.siblings) ? opts.ajaxURL.siblings(nodeData) : opts.ajaxURL.siblings + nodeId) :
754+
($.isFunction(opts.ajaxURL.families) ? opts.ajaxURL.families(nodeData) : opts.ajaxURL.families + nodeId);
753755
if (startLoading($that, $node, opts)) {
754-
$.ajax({ 'url': url + nodeId + '/', 'dataType': 'json' })
756+
$.ajax({ 'url': url, 'dataType': 'json' })
755757
.done(function(data, textStatus, jqXHR) {
756758
if ($node.closest('.orgchart').data('inAjax')) {
757759
if (data.siblings || data.children) {

examples/ondemand-loading-data/scripts.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
$(function() {
66

77
$.mockjax({
8-
url: '/orgchart/children/3/',
8+
url: '/orgchart/children/3',
99
contentType: 'application/json',
1010
responseTime: 1000,
1111
responseText: { 'children': [
@@ -15,14 +15,14 @@
1515
});
1616

1717
$.mockjax({
18-
url: '/orgchart/parent/1/',
18+
url: '/orgchart/parent/1',
1919
contentType: 'application/json',
2020
responseTime: 1000,
2121
responseText: { 'id': '6','name': 'Lao Lao', 'title': 'general manager', 'relationship': '001' }
2222
});
2323

2424
$.mockjax({
25-
url: '/orgchart/siblings/1/',
25+
url: '/orgchart/siblings/1',
2626
contentType: 'application/json',
2727
responseTime: 1000,
2828
responseText: { 'siblings': [
@@ -37,7 +37,7 @@
3737
});
3838

3939
$.mockjax({
40-
url: '/orgchart/families/1/',
40+
url: '/orgchart/families/1',
4141
contentType: 'application/json',
4242
responseTime: 1000,
4343
responseText: {
@@ -70,8 +70,12 @@
7070
var ajaxURLs = {
7171
'children': '/orgchart/children/',
7272
'parent': '/orgchart/parent/',
73-
'siblings': '/orgchart/siblings/',
74-
'families': '/orgchart/families/'
73+
'siblings': function(nodeData) {
74+
return '/orgchart/siblings/' + nodeData.id;
75+
},
76+
'families': function(nodeData) {
77+
return '/orgchart/families/' + nodeData.id;
78+
}
7579
};
7680

7781
$('#chart-container').orgchart({

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "orgchart",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"description": "Simple and direct organization chart(tree-like hierarchy) plugin based on pure DOM and jQuery.",
55
"main": "./dist/js/jquery.orgchart.js",
66
"style": [

0 commit comments

Comments
 (0)