Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix error_callback usage. Add custom_settings to dajaxice calls to cu…
…stomize timeouts, etc...
  • Loading branch information
Jorge Bastida committed May 31, 2011
1 parent e626f8d commit c4e1232
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 50 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Expand Up @@ -3,8 +3,9 @@ dajaxice (0.2) stable; urgency=normal
- Dajaxice now propagate docstrings to javascript dajaxice functions.
- Added DAJAXICE_JS_DOCSTRINGS to configure docstrings propagation behaviour, default=False.
- Documentation Updated.
- Fix race condition with the 'is_callback_a_function' variable in dajaxice.core.js
- Fix bug with the 'is_callback_a_function' variable in dajaxice.core.js
- Updated installation guide for compatibility with django 1.3
- Exception handling was fully rewritten. New error_callback and default_error_callback.

-- Benito Jorge Bastida <me@jorgebastida.com> Sun, 25 May 2010 10:58:00 +0000

Expand Down
14 changes: 10 additions & 4 deletions dajaxice/templates/dajaxice/dajaxice.core.js
Expand Up @@ -20,16 +20,22 @@ var Dajaxice = {
return cookieValue;
},

call: function(dajaxice_function, dajaxice_callback, argv, exception_callback)
call: function(dajaxice_function, dajaxice_callback, argv, custom_settings)
{
var send_data = [];
var is_callback_a_function = (typeof(dajaxice_callback) == 'function');

if(!is_callback_a_function){
alert("dajaxice_callback should be a function since dajaxice 0.2")
}

if(exception_callback==undefined || typeof(dajaxice_callback) != 'function'){
exception_callback = this.get_setting('default_exception_callback');
if(custom_settings == undefined){
custom_settings = {};
}

var error_callback = this.get_setting('default_exception_callback');
if('error_callback' in custom_settings && typeof(custom_settings['error_callback']) == 'function'){
error_callback = custom_settings['error_callback'];
}

send_data.push('argv='+encodeURIComponent(JSON.stringify(argv)));
Expand All @@ -41,7 +47,7 @@ var Dajaxice = {
oXMLHttpRequest.onreadystatechange = function() {
if (this.readyState == XMLHttpRequest.DONE) {
if(this.responseText == Dajaxice.EXCEPTION){
exception_callback();
error_callback();
}
else{
try{
Expand Down
4 changes: 2 additions & 2 deletions dajaxice/templates/dajaxice/dajaxice_core_loop.js
@@ -1,8 +1,8 @@
{{ module.name }}: {
{% for function in module.functions %}
{% if function.doc and DAJAXICE_JS_DOCSTRINGS %}/* {{ function.doc|default:'' }}*/ {% endif %}
{{ function.name }}: function(callback_function, argv, exception_callback){
Dajaxice.call('{{function.get_callable_path}}', callback_function, argv, exception_callback);
{{ function.name }}: function(callback_function, argv, custom_settings){
Dajaxice.call('{{function.get_callable_path}}', callback_function, argv, custom_settings);
}{% if not forloop.last %},{% endif %}
{% endfor %}

Expand Down
63 changes: 20 additions & 43 deletions examples/simple/templates/simple/simple_index.html
Expand Up @@ -8,69 +8,46 @@
<script type="text/javascript" charset="utf-8">

function callback_example1(data){
if(data!=Dajaxice.EXCEPTION){
alert(data.message);
}
else{
alert('Error');
}
alert(data.message);
}

function callback_example2(data){
if(data!=Dajaxice.EXCEPTION){
for (var i=0; i < data.numbers.length; i++) {
alert(data.numbers[i]);
}
}
else{
alert('Error');
for (var i=0; i < data.numbers.length; i++) {
alert(data.numbers[i]);
}
}

function callback_example3(data){
if(data!=Dajaxice.EXCEPTION){
alert(data.result);
}
else{
alert('Error');
}
alert(data.result);
}

function callback_example_error(data){
if(data!=Dajaxice.EXCEPTION){
alert(data);
}
else{
alert('Error');
}
alert(data);
}

function callback_complex_example1(data){
if(data!=Dajaxice.EXCEPTION){
alert(data.message);
}
else{
alert('Error');
}
alert(data.message);
}

function custom_error(){
alert('Custom error');
}

</script>
</head>
<body>
<h2>dajaxice examples</h2>
<h3>old string callback</h3>
<input type="button" name="some_name" value="Call Example 1! (String)" onclick="Dajaxice.simple.example1('callback_example1');" id="some_name" /><br/>
<input type="button" name="some_name" value="Call Example 2! (Number list)" onclick="Dajaxice.simple.example2('callback_example2');" id="some_name" /><br/>
<input type="button" name="some_name" value="Call Example 3! (Sending Array)" onclick="Dajaxice.simple.example3('callback_example3', {'data':[1,2,3],'name':'Peter'});" id="some_name"/><br/>
<input type="button" name="some_name" value="Raise Exception" onclick="Dajaxice.simple.error_example('callback_example_error');" id="some_name" /><br/>
<input type="button" name="some_name" value="Complex Example" onclick="Dajaxice.complex.second.complex_example1('callback_complex_example1');" id="some_name" />

<h3>new function callback</h3>
<input type="button" name="some_name" value="Call Example 1! (String)" onclick="Dajaxice.simple.example1(callback_example1);" id="some_name" /><br/>
<input type="button" name="some_name" value="Call Example 2! (Number list)" onclick="Dajaxice.simple.example2(callback_example2);" id="some_name" /><br/>
<input type="button" name="some_name" value="Call Example 3! (Sending Array)" onclick="Dajaxice.simple.example3(callback_example3, {'data':[1,2,3],'name':'Peter'});" id="some_name"/><br/>
<input type="button" name="some_name" value="Raise Exception" onclick="Dajaxice.simple.error_example(callback_example_error);" id="some_name" /><br/>
<input type="button" name="some_name" value="Raise Exception with custom exception callback" onclick="Dajaxice.simple.error_example(callback_example_error,{},function(){alert('custom error')});" id="some_name" /><br/>
<input type="button" name="some_name" value="Complex Example" onclick="Dajaxice.complex.second.complex_example1(callback_complex_example1);" id="some_name" />
<input type="button" value="Call Example 1! (String)" onclick="Dajaxice.simple.example1(callback_example1);" /><br/>
<input type="button" value="Call Example 2! (Number list)" onclick="Dajaxice.simple.example2(callback_example2);" /><br/>
<input type="button" value="Call Example 3! (Sending Array)" onclick="Dajaxice.simple.example3(callback_example3, {'data':[1,2,3],'name':'Peter'});" /><br/>
<input type="button" value="Raise Exception" onclick="Dajaxice.simple.error_example(callback_example_error);" /><br/>
<input type="button" value="Raise Exception with custom exception callback" onclick="Dajaxice.simple.error_example(callback_example_error, {},{'error_callback': custom_error});" /><br/>
<input type="button" value="Complex Example" onclick="Dajaxice.complex.second.complex_example1(callback_complex_example1);" />

<h3>old string callback</h3>
<input type="button" name="some_name" value="Call Example 1! (String)" onclick="Dajaxice.simple.example1('callback_example1');"

</body>
</html>

0 comments on commit c4e1232

Please sign in to comment.