juek / CustomSections

Developer plugin for rapid prototyping of custom section types in Typesetter CMS
GNU General Public License v2.0
3 stars 7 forks source link

Section reaction on postC() #42

Closed mahotilo closed 6 years ago

mahotilo commented 6 years ago

Hi, Juergen! I continue my work on shop cart section:) I have integrated code from run_custom_scripts and its does works. Thank you! But I have some problem with my modal wizard (user registration). So I discovered, that after sending POST with postC() TS "recreate" my section. Precisely, what I see:

  1. I have some Badge (<span>) in my section with default value ZZZ
  2. JS from section.php $js_files initialize Badge with current products quantity. (So I even don't see ZZZ on Badge when open page)
  3. On click on Badge I call postC()
  4. (Here I have broke my wizard hiding process. It is my problem, and I see the way to fix it. I suppose so:). And when all freezed I suddenly see ZZZ on my Badge!

It means that section was recreated or $section['content'] was re-shown without re-run scripts? For now it bring a mess to in events flow on my section. Is it predefined behaviour after postC() or some kind of error behaveour?

mahotilo commented 6 years ago

Oh, I have solved problem with events mess, so my modal now hides correctly without lost .modal-backdrop. But as I said I still see Badge with default value after postC(). Edit And I have lost all my on() events from script.js. It is a show-stopper for me :(

juek commented 6 years ago

It means that section was recreated or $section['content'] was re-shown without re-run scripts?

Sounds as if the AJAX process replaces the page content (#gpx_content) with a new one, which is the default response of $gp.postC, if not specified otherwise. Open the browser's Inspector -> Network Tab and watch the Post parameters and response.

mahotilo commented 6 years ago

Yes I see DO:"replace" in response. Where I can learn about $gp.postC parameters. Searching in /include does not help me.

// Headers -------------------------------------------------------

Request URL:http://www.test:8080/test
Request Method:POST
Status Code:200 OK
Remote Address:127.0.0.1:8080
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Cache-Control:post-check=0, pre-check=0
Cache-Control:no-store, no-cache, must-revalidate
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:2202
Content-Type:text/html; charset=utf-8
Date:Sat, 26 Aug 2017 16:08:32 GMT
Expires:Sat, 26 Aug 2017 16:08:33 GMT
Keep-Alive:timeout=5, max=100
Last-Modified:Sat, 26 Aug 2017 16:08:33 GMT
Pragma:no-cache
Server:Apache/2.4.27 (Win32) PHP/7.1.7
Vary:Accept-Encoding
X-Powered-By:PHP/7.1.7
Request Headers
view source
Accept:text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:uk-UA,uk;q=0.8,ru;q=0.6,en-US;q=0.4,en;q=0.2
Connection:keep-alive
Content-Length:1642
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:cookies_accepted=1; cart[uid]=595d01072d56b; country_code=UA; zone_code=71; gpEasy_029e489cfcdc=Bj07BUKAqJx6zA0INFHqg1lBkIGZ1guMk8LIu9vI; g=2; gpEasy_8435a22e172b=b7fcTfrgFn6M2gQ5cDek3owljmYjhEt2bfAkBzsF
DNT:1
Host:www.test:8080
Origin:http://www.test:8080
Referer:http://www.test:8080/test
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 OPR/47.0.2631.71
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
cmd:custom_sections_cmd
type:my_shop_cart
Name:1111111
Phone:(111) 111-11-11
CartData:(unable to decode value)
verified:1433e8c307dba94e2e576c8ad2a636008c2ebcdf65dd3ee9cac9c968bc430ff81aafb7d69d118c0ba96e441074e27049b8666efa97b1714fed671a71dcd47e9c
gpreq:json
jsoncallback:jQuery111301170884230686251_1503763687461
Name
test

// Responce --------------------------------------------------- jQuery111301170884230686251_1503763687461([{DO:"replace",SELECTOR:"#gpx_content",CONTENT:"<div id=\"gpx_content\" .....

juek commented 6 years ago

The post seems ok except CartData which needs to be encoded/parametrized. The response replaces #gpx_content, as I suspected. Have you reset cleared the ajaxReplace Array in script.php? $page->ajaxReplace = array();

Where I can learn about $gp.postC parameters.

It's in main.js

BTW: There is no need to use $gp.postC — it's just a wrapper for jQuery.post which pre-processes the data, adds the required nonce and a default callback ($gp.Response). Can be done different. It's just convenient for standard content replacement.

mahotilo commented 6 years ago

Have you reset the ajaxReplace Array in script.php? $page->ajaxReplace = array();

That is it! Oh, I am dummkopf. I have accidentally removed it with other vars. I have compared script.php from yours run_custom_scripts and my one dozen times, and miss this difference. Thank you, Juergen.

Can you mark this line like // MANDATORY!

And if we already talking about script.php. In script.php we have return 'return'; Is it mandatory string too, or I can change it and receive it in script.js? I really want to receive from script.php status of my mail sending to decide whether I can clear cart after sending or not.

juek commented 6 years ago

Can you mark this line like // MANDATORY!

It depends. For any simple request the ajaxReplace array should be cleared. But of course we can also add more than a single replace action to the callback/response and the server-side processing could call several methods/functions, each one adding data to the ajaxReplace array (e.g. the yellow-bar-messages are also added this way.)

Edit: Clearing the ajaxReplace array will remove any previously added action, so we have to be cautious, where/when we do it. (BTW: I formerly used 'reset', which is not the correct term. Resetting an array has a different meaning in PHP.)

In script.php we have return 'return'; Is it mandatory string too, or I can change it and receive it in script.js?

Returning 'return' just tells Typesetter that everything needed is already done with the PageRunScript call, so Typesetter can skip rendering other stuff (it doesn't even have to load the page content). See here

mahotilo commented 6 years ago

Thx. My PHP, AJAX skill have significantly grow up during last week but still is too low:)

juek commented 6 years ago

Just another note. In general, be careful replacing section content via AJAX in editing mode or it might get saved, depending on how the editor is set up.

mahotilo commented 6 years ago

or it might get saved

Yes, it will be awesome "surprise". Thx!