views:

38

answers:

1

Is this a valid function pointer code below,

In views ,

    def change(request):
       dict={}
       function_ptr="create()" 

        dict.update({'function_ptr' : function_ptr})
       return render_to_response('mpjt/create.html',context_instance=RequestContext(request,{'dict': dict}))

In create.html

      $(document).ready(function() {
     var a = '{{dict.function_ptr}}'

     func_ptr(a);

      function create()
     {
       alert('got respponse');
      }
     });

Thanks..

+1  A: 

No.

Pass function_ptr='create' in your python code and use the following in your JavaScript:

var func_ptr = window[{{ dict.function_ptr }}];

This must be done AFTER the function create() has been defined! If you want to do it earlier, you could do it with an anonymous function:

var func_ptr = function() {
    return window[{{ dict.function_ptr }}];
}
ThiefMaster
Can u please explain how is it different?
Hulk
window['funcname'] is the proper way to get the function pointer from a string. A string containing 'funcname' or 'funcname()' is just a plain string - not a function (pointer). And while you could execute 'funcname()' with eval() that's very dirty ("eval is evil")
ThiefMaster
Whoever downvoted it: What about a comment? It's pretty rude to downvote a correct answer without mentioning a reason.
ThiefMaster
Thanks.................
Hulk