Announcement

Collapse
No announcement yet.

Help on Calling Module Function from Javascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help on Calling Module Function from Javascript

    Module:
    Code:
    <MvFUNCTION NAME="Module_JSON" PARAMETERS="module var" STANDARDOUTPUTLEVEL="">
        <MvIF EXPR = "{ g.Module_Function EQ 'MyFunction' }">  <MvFUNCTIONRETURN VALUE = "{ JSON_MyFunction( l.module ) }">
        </MvIF>
    
         <MvFUNCTIONRETURN VALUE = 1>
    </MvFUNCTION>
    
    <MvFUNCTION NAME = "JSON_MyFunction" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
        <MvASSIGN NAME="g.param" VALUE="{ trim(g.param) }">
        --- Do something here using param
        <MvFUNCTIONRETURN VALUE = 1>
    </MvFUNCTION>
    
    <MvFUNCTION NAME = "Module_Customer_Content" PARAMETERS = "module var, tab, load_fields, customer var" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
    <script language="JavaScript">
     function MyFunc(param)
     {
          var callback;
          call_response = My_Function( param, callback );
     }
    
     function My_Function( param, callback )
     {
      return AJAX_Call_Module( callback,
                                                 'admin',
                                                 '<MvEVAL EXPR = "{ g.Encoded_Module_Code }">',
                                                 'MyFunction',
                                                 'param='  + encodeURIComponent( param )
                                               );
     }
    </script>
    
    -- onclick = "JavaScript:MyFunc(param)"
    In some reason, JSON_My_Function is not executed.
    What is wrong with this code?
    Last edited by compumate99; 07-27-16, 03:48 PM.
    Daniel Kim, Compu-Mate
    Developer

    #2
    I don't see where you are calling Module_JSON. You do that by calling the file json.mvc instead of merchant.mvc.

    From the Miva Merchant 9 Module API Reference Guide:

    Module functions are accessed through a URL to json.mvc with the following parameters:
    Function – String (required). Must always be set to Module.
    Module_Code – String (required). The code of the module that provides the JSON function.
    Module_Function – String (optional). A value that indicates to the module what function should be performed. Made available to the module as g.Module_Function.
    Session_Type – String (optional). If not specified, json.mvc does no session verification. If “runtime”, json.mvc will load a shopping interface basket, if one exists, or create a new one. If
    “admin”, json.mvc will verify that the caller has valid administrative interface credentials before calling the module.


    These parameters (if sent correctly, which it doesn't look like you are doing) will show up inside Module_JSON as globals (e.g. g.Function). If g.Function != the string module then nothing will pass. I don't see where you are setting and passing that, as just one example. You seem to be trying to use Session_Type of admin, which means you also need to pass in g.Session_ID or else you will never be validated! So you are missing two pieces of data, either one of which ensures your code will never run.

    I'm not sure why you are using AJAX_Call_Module. (Does anyone else outside of Miva use that? I looked at it years ago and, while it may be useful internally, it never made sense to me.)

    The framework I use is:

    1. JavaScript event listener (I use jQuery because it is superior in every way for doing AJAX and JSON) attached to some trigger that calls higher level JS function
    2. A ton of my standard jQuery functions that all get called (from the above high level function) to preprocess, prep and build the AJAX stack, send the AJAX to json.mvc
    3. A standard Module_JSON function that calls my custom DoAJAX MivaScript function. This is like a switchboard and EVERYTHING from the UI layer (admin or runtime) talks to/receives response from custom MivaScript functions. (These days my modules tend to be 10% HTML/CSS, 30-40% MivaScript functions, and the rest is jQuery. LOTS of jQuery.)
    4. Everything is returned to the UI layer as JSON, unless I need super-specific HTML.
    5. More JS (jQuery) functions to handle the JSON response and create/modify HTML/CSS in the page.

    Gordon Currie
    Phosphor Media - "Your Success is our Business"

    Improve Your Customer Service | Get MORE Customers | Edit Any Document Easily | Free Modules | Follow Us on Facebook
    phosphormedia.com

    Comment


      #3
      My understanding was that AJAX_Call_module will send function call package to Module_JSON function and JSON module will call Miva function obtained from first statement in module function. Is this wrong?
      Daniel Kim, Compu-Mate
      Developer

      Comment


        #4
        As far as I can see, yes. It appears to conflict directly with the excerpt I posted above. But if someone from Miva could explain to you how to use it, great. That won't be me, since I do not know AJAX_Call_Module.
        Gordon Currie
        Phosphor Media - "Your Success is our Business"

        Improve Your Customer Service | Get MORE Customers | Edit Any Document Easily | Free Modules | Follow Us on Facebook
        phosphormedia.com

        Comment


          #5
          Thanks for clarification, Gordon.
          Daniel Kim, Compu-Mate
          Developer

          Comment


            #6
            In your JavaScript function, you are passing "MyFunction" as the function name, but in Module_JSON, you are looking for "My_Function".

            Comment


              #7
              I am sorry for confusion.
              This is typo when I changed actual function name with sample function name.

              It is MyFunction everywhere.
              Daniel Kim, Compu-Mate
              Developer

              Comment


                #8
                Module feature : json, vis_cust, etc...............

                Code:
                <MvFUNCTION NAME="Module_JSON" PARAMETERS="module var" STANDARDOUTPUTLEVEL="">
                    <MvIF EXPR = "{ g.Module_Function EQ 'UpsAddressList_Load_Query' }">  <MvFUNCTIONRETURN VALUE = "{ JSON_UpsAddressList_Load_Query( l.module ) }">
                    <MvELSEIF EXPR = "{ g.Module_Function EQ 'UpsAddress_Insert' }">   <MvFUNCTIONRETURN VALUE = "{ JSON_UpsAddress_Insert( l.module ) }">
                    <MvELSEIF EXPR = "{ g.Module_Function EQ 'UpsAddress_Update' }">   <MvFUNCTIONRETURN VALUE = "{ JSON_UpsAddress_Update( l.module ) }">
                    <MvELSEIF EXPR = "{ g.Module_Function EQ 'UpsAddress_Update_Field' }">  <MvFUNCTIONRETURN VALUE = "{ JSON_UpsAddress_Update_Field( l.module ) }">
                    <MvELSEIF EXPR = "{ g.Module_Function EQ 'UpsAddress_Delete' }">   <MvFUNCTIONRETURN VALUE = "{ JSON_UpsAddress_Delete( l.module ) }">
                    </MvIF>
                
                    <MvFUNCTIONRETURN VALUE = 1>
                </MvFUNCTION>
                
                <MvFUNCTION NAME = "JSON_UpsAddress_Update_Field" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
                    <MvIF EXPR = "{ g.Session_Type NE 'admin' }">       <MvFUNCTIONRETURN> </MvIF>
                        <MvIF EXPR = "{ NOT [ g.Module_JSON ].JSON_Store_Open() }">    <MvFUNCTIONRETURN> </MvIF>
                        <MvIF EXPR = "{ NOT [ g.Module_Admin ].CanI( 'CUST', 0, 0, 1, 0 ) }"> <MvFUNCTIONRETURN> </MvIF>
                
                        <MvQUERY NAME = "Merchant"
                                            QUERY = "{ 'UPDATE ' $ g.Store_Table_Prefix $ 'CustomerAddresses
                                                                 SET ?  = ?
                                                                 WHERE id = ?' }"
                                            FIELDS = "g.fldname,
                                                              g.fldvalue,
                                                              g.addr_id">
                        <MvIF EXPR = "{ g.MvQUERY_Error }">
                            <MvFUNCTIONRETURN VALUE = "{ [ g.Module_Library_Utilities ].Error( 'MER-BCH-TBR-00014', g.MvQUERY_Error ) }">
                        </MvIF>
                
                        <MvFUNCTIONRETURN VALUE = 1>
                </MvFUNCTION>
                
                <MvFUNCTION NAME = "Module_Customer_Content" PARAMETERS = "module var, tab, load_fields, customer var" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
                
                .
                .
                Some MIVA scripts
                .
                .
                
                <script language="JavaScript">
                
                 function changeflag(addrId, fldname, fldvalue)
                 {//javascript fired when button is clicked.
                  if ( fldvalue == 0 ) {
                   fldvalue = 1;
                  }
                  else {
                   fldvalue = 0;
                  }
                  var callback;
                  call_response = UpsAddress_Update_Field( addrId, fldname, fldvalue, callback );
                  alert(call_response.responseXML);
                 }
                
                 function UpsAddress_Update_Field( addr_id, fldname, fldvalue, callback )
                 {
                  return AJAX_Call_Module( callback,
                         'admin',
                         '<MvEVAL EXPR = "{ 'g.Encoded_Module_Code' }">',
                         'UpsAddress_Update_Field',
                         'addr_id='  + encodeURIComponent( addr_id ) +
                         '&fldname=' + encodeURIComponent( fldname ) +
                         '&fldvalue=' + encodeURIComponent( fldvalue )
                  );
                 }
                
                </script>
                .
                .
                Other MIVA script lines here
                .
                .
                
                
                    <MvFUNCTIONRETURN VALUE = 1>
                </MvFUNCTION>
                The fowwloing has been verified.

                function AJAX_Call_LowLevel is executed with
                addr_id=7211&fldname-resdntl&fldvalue=0&Session_Type=admin&Session_ID=. ....................... as content
                Store_Code=1&Function=Module&Module_Code=&Module_F unction=UpsAddress_Update_Field as uri

                Return value from AJAX_Call_LowLevel call_response.responseXML evaluates to 'null' and MySQL data was not updated.

                I don't think JSON function is fired. Is there way I can check whether JSON function is fired or not?
                What can go wrong?

                Thank you
                Daniel Kim, Compu-Mate
                Developer

                Comment


                  #9
                  Do you have "json" listed as one of your module features in Module_Description? Have you updated the module at the domain level so that the "json" feature shows there?

                  Comment


                    #10
                    Yes, json is listed as one of module feature. I do not understand what you mean by updating the module at the domain level.
                    Do you mean this?

                    module.png
                    Daniel Kim, Compu-Mate
                    Developer

                    Comment


                      #11
                      I figured out.
                      callback function was not defined.

                      JSON function is fired and executed successfully after callback function is defined.

                      Database is updated successfully and module function returns normally with the following statement.

                      Thank you.
                      Attached Files
                      Last edited by compumate99; 08-15-16, 12:39 PM.
                      Daniel Kim, Compu-Mate
                      Developer

                      Comment

                      Working...
                      X