简体   繁体   中英

django dajaxice javascript callback with additional parameters

How do I go about adding extra parameters on the call back function?

eg.

function callback(data){
    alert(data.message);
}

And you would call it by Dajaxice.examples.args_example(callback, {'text':$('#text').val()}

what if I want to add an extra parameter in the callback function

function callback(data, flag){
    alert(data.message);
    if (flag){ /* do something */}
}

Currently I have a work around like this:

Dajaxice.examples.args_example(function(data){
    callback(data, true);
}, {'text':$('#text').val()});

Is there a better way or official way to do it in dajaxice?

Am I correct to assume this flag will depend on something from within the ajax function?

If so, could you not pass your flag as a part of the returned json ?

function callback(data) {
    alert(data.message);
    if (data.flag) { /* do something */}
}

In your ajax.py file:

def my_ajax_func(... my_args ...):
...
    my_message = "Hello world!"
    my_flag = True
    return simplejson.dumps({'message':my_message, 'flag':my_flag})

In your javascript callback function:

function callback(data){
    alert(data.message);
    if (data.flag){ /* do something */}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM