You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

86 line
2.4 KiB

  1. Client Side Scripts
  2. ===================
  3. Introduction
  4. ------------
  5. Client side scripts are written in javascript and they are used to execute function on event triggers.
  6. These scripts can be used to:
  7. * Manipulate views
  8. * Do server calls (AJAX)
  9. * Validate entries
  10. * Do calculations
  11. Client scripts can be written inside of DocType in the "Client Script" tab
  12. cur_frm Object
  13. --------------
  14. All client script functions are attached to the cur_frm.cscript object. This object holds the functions
  15. relating to a DocType (form)
  16. onload event
  17. ------------
  18. onload is called when the a record is loaded for the first time. Example::
  19. cur_frm.cscript.onload = function(doc, doctype, docname) {
  20. if(!doc.from) {
  21. doc.from_user = user;
  22. refresh_field('user')
  23. }
  24. }
  25. refersh event
  26. -------------
  27. refresh event is similar to the onload event. Except that it is also called each time the page is refreshed
  28. either via a user refresh, save or otherwise.
  29. Server Calls
  30. ------------
  31. A typical use is to get/set data at server side. To do this, the framework has built-in AJAX interface using
  32. the $c_obj (call server object) function. The typical pattern is as follows::
  33. // function will be called when the server responds
  34. var callback = function(response, responseInText) {
  35. // set the new value
  36. // re-assign the doc record because you are inside the callback
  37. var doc = locals[doc.doctype][doc.docname]
  38. doc.new_value = response.message
  39. refresh_field('new_value');
  40. }
  41. // call this object on the server
  42. $c_obj([doc], 'get_my_value', doc.based_on, callback);
  43. Field Level Triggers
  44. --------------------
  45. You can set functions to be called when values are changed in the form, at the "onchange" event.
  46. To set a trigger, in the Field table, set the value of the Trigger column to "Client".
  47. Declare a function to be called by its fieldname::
  48. // attach a trigger on the "my_value" field
  49. cur_frm.cscript.my_value = function(doc, doctype, docname) {
  50. msgrint("My value has been changed")
  51. // do something
  52. }
  53. Fetch Pattern
  54. -------------
  55. Another typical pattern is to get values based on other values, like when you select a Customer, its type and
  56. contact info should come automatically, to do this, you can use the standard fetch pattern on link fields::
  57. // add_fetch(link, source_field_name, target_field_name)
  58. add_fetch('customer', 'contact_details', 'contact_details')