Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
25 posts Page 3 of 3
madebym wrote: I know you provided a modified version of the code in this thread, that keeps the user on the same page, but I am using a separate template to show the shopping bag items, so that code doesn't work in my case. It stays on the same page, but the shopping bag page doesn't get populated, of course, beacuse the href attribute is now pointing to the current page we are on.
The add/remove links do not just point to the same page - they have the query string parameters included to modify the shopping bag contents. Additionally, it is not consequential whether you have a dedicated template to list the shopping bag items; you will still be able to display that list anywhere else.

Moving on to AJAX... In the example code we just need to perform a GET request, not a POST. Ampersands should not be encoded when written in JavaScript. Nevertheless, we do not need to manually hard-code the URL because it is already present in the href attribute of the add link.
Code: Select all
$(".add-to-design-board").on("click", function(event) {
    event.preventDefault();

    $.ajax({
        url: $(this).attr("href")
    }).done(function() {
        // Do something
    });
});
It is up to you to decide what action you want to take once the request is completed.

This is really outside the scope of Couch, so I can't offer more than that.
That piece of code you provided was all I needed :)

I am very bad at explaining what I need, also, english is not my native language, so I complicated things.

All that I needed was that few lines of JS you provided.

Thanks !!
Hi Couchers,
So to show the recently viewed pages on the home page I did this:
Code: Select all
<cms:if "<cms:gpc var='clear' method='get'/>" || setting == '-1'>
        <cms:delete_cookie name='recent'/>
</cms:if>


<cms:set recently_viewed="<cms:get_cookie name='recent'/>" scope='global' />

<ol>
            <cms:set id_count = '0'/>
            <cms:each var=recently_viewed as='id' sep=','>
                <cms:set id_count = "<cms:add id_count '1'/>" scope='parent'/>
                     <cms:pages masterpage='shop.php' id=id limit='1' >
                        <li><a href="<cms:show k_page_link/>"><cms:show k_page_title/></a></li>
                    </cms:pages>
            </cms:each>
</ol>

<cms:if recently_viewed && "cms:not_empty recently_viewed />" >
        <p><a href="<cms:add_querystring link=k_page_link querystring='clear=1'/>">Clear Recently Viewed List</a></p>

        <cms:if (privacy || setting == '-1') && setting != '1'>
        <p><a href="<cms:add_querystring link=k_page_link querystring='setting=1'/>">Enable Recently Viewed List</a></p>
    <cms:else/>
        <p><a href="<cms:add_querystring link=k_page_link querystring='setting=-1'/>">Disable Recently Viewed List</a></p>
    </cms:if>
        </cms:if>


When I try to clear the recently viewed items, 'clear=1' is set alright but the items are not deleted unless I refresh the page. So I want to know, should I do it this way and is it the best practice?
Code: Select all
<cms:if "<cms:gpc var='clear' method='get'/>" || setting == '-1'>
        <cms:delete_cookie name='recent'/>
        <cms:redirect url=k_page_link />
    </cms:if>


<cms:set recently_viewed="<cms:get_cookie name='recent'/>" scope='global' />

<ol>
            <cms:set id_count = '0'/>
            <cms:each var=recently_viewed as='id' sep=','>
                <cms:set id_count = "<cms:add id_count '1'/>" scope='parent'/>
                     <cms:pages masterpage='shop.php' id=id limit='1' >
                        <li><a href="<cms:show k_page_link/>"><cms:show k_page_title/></a></li>
                    </cms:pages>
            </cms:each>
</ol>

<cms:if recently_viewed && "cms:not_empty recently_viewed />" >
        <p><a href="<cms:add_querystring link=k_page_link querystring='clear=1'/>">Clear Recently Viewed List</a></p>

        <cms:if (privacy || setting == '-1') && setting != '1'>
        <p><a href="<cms:add_querystring link=k_page_link querystring='setting=1'/>">Enable Recently Viewed List</a></p>
    <cms:else/>
        <p><a href="<cms:add_querystring link=k_page_link querystring='setting=-1'/>">Disable Recently Viewed List</a></p>
    </cms:if>
        </cms:if>


Thank you
Instead of deleting a cookie, try to set it empty. Not 100% sure that it would solve the refresh issue, but worth a try.
Update: I found a solution:
Code: Select all
<cms:if "<cms:gpc var='clear' method='get'/>" || setting == '-1'>

        <cms:delete_cookie name='recent'/>

        <cms:else/>

        <cms:set recently_viewed="<cms:get_cookie name='recent'/>" scope='global' />

    </cms:if>


Thanks trendoman, that was exactly what I did. Thanks for your suggestion anyway.
25 posts Page 3 of 3