Skip to content

Commit 21fb9b6

Browse files
committed
Allow customizable js initialization
1 parent 2bb41ee commit 21fb9b6

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

djangojs/static/js/djangojs/django.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,22 @@
1717
/**
1818
* Initialize required attributes
1919
*/
20-
initialize: function() {
21-
this.urls = window.DJANGO_JS_URLS;
22-
this.set_context(window.DJANGO_JS_CONTEXT);
20+
initialize: function(params) {
21+
params = params || {};
22+
if (typeof params.urls === 'string' || params.urls instanceof String) {
23+
$.get(params.urls, function(urls) {
24+
Django.urls = urls;
25+
});
26+
} else {
27+
this.urls = params.urls || window.DJANGO_JS_URLS;
28+
}
29+
if (typeof params.context === 'string' || params.context instanceof String) {
30+
$.get(params.context, function(context) {
31+
Django.set_context(context);
32+
});
33+
} else {
34+
this.set_context(params.context || window.DJANGO_JS_CONTEXT);
35+
}
2336
},
2437

2538
set_context: function(context) {
@@ -41,7 +54,9 @@
4154
reload: function(callback) {
4255
$.get(this.url('django_js_context'), function(context) {
4356
Django.set_context(context);
44-
callback();
57+
if (callback instanceof Function) {
58+
callback();
59+
}
4560
});
4661
},
4762

djangojs/static/js/test/django.specs.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,90 @@ describe("Django.js", function() {
1010
expect(Django).toBeDefined();
1111
});
1212

13+
describe('Initialization', function() {
14+
var backup_context, backup_urls;
15+
16+
beforeEach(function() {
17+
backup_context = window.DJANGO_JS_CONTEXT;
18+
backup_urls = window.DJANGO_JS_URLS;
19+
});
20+
21+
afterEach(function() {
22+
window.DJANGO_JS_CONTEXT = backup_context;
23+
window.DJANGO_JS_URLS = backup_urls;
24+
Django.initialize();
25+
});
26+
27+
it('can be initialized without parameter from global vars content', function() {
28+
Django.initialize();
29+
expect(Django.context).toEqual(window.DJANGO_JS_CONTEXT);
30+
expect(Django.urls).toEqual(window.DJANGO_JS_URLS);
31+
});
32+
33+
describe('can be initialized with an optionnal object parameter', function() {
34+
it('with an "urls" attribute containing json', function() {
35+
var urls = {'my-url': '/my-url'};
36+
37+
Django.initialize({urls: urls});
38+
39+
expect(Django.urls).toEqual(urls);
40+
expect(Django.context).toEqual(window.DJANGO_JS_CONTEXT);
41+
});
42+
43+
it('with a "context" attribute containing json', function() {
44+
var context = {'fake': true};
45+
46+
Django.initialize({context: context});
47+
48+
expect(Django.context).toEqual(context);
49+
expect(Django.urls).toEqual(window.DJANGO_JS_URLS);
50+
});
51+
52+
it('with an "urls" attribute containing a remote url', function() {
53+
var urls = {'my-url': '/my-url'};
54+
spyOn($, "ajax").andCallFake(function(params) {
55+
expect(params.url).toEqual('http://somewhere');
56+
params.success(urls);
57+
});
58+
59+
Django.initialize({urls: 'http://somewhere'});
60+
61+
expect(Django.urls).toEqual(urls);
62+
expect(Django.context).toEqual(window.DJANGO_JS_CONTEXT);
63+
});
64+
65+
it('with a "context" attribute containing a remote url', function() {
66+
var context = {'fake': true};
67+
spyOn($, "ajax").andCallFake(function(params) {
68+
expect(params.url).toEqual('http://somewhere');
69+
params.success(context);
70+
});
71+
72+
Django.initialize({context: 'http://somewhere'});
73+
74+
expect(Django.context).toEqual(context);
75+
expect(Django.urls).toEqual(window.DJANGO_JS_URLS);
76+
});
77+
78+
it('with any combination of both json and a remote url', function() {
79+
var urls = {'my-url': '/my-url'},
80+
context = {'fake': true};
81+
82+
spyOn($, "ajax").andCallFake(function(params) {
83+
expect(params.url).toEqual('http://somewhere');
84+
params.success(urls);
85+
});
86+
87+
Django.initialize({urls: 'http://somewhere', context: context});
88+
89+
expect(Django.urls).toEqual(urls);
90+
expect(Django.context).toEqual(context);
91+
});
92+
93+
});
94+
95+
})
96+
1397
describe('Context', function() {
1498
it('have a context attribute', function() {
1599
expect(Django.context).toBeDefined();

0 commit comments

Comments
 (0)