Best way to set cookies?


(Pomster09) #1

Hi guys, I want my users to be able to save a favourites list.

 

So I have a bunch of pages (about 30) with a table of data, and other content on them. The data is pretty important, and an average user will normally access the content of at least 5 different tables on a frequent basis.

 

I'm wanting to set up a "My favourites" type page, which will only display the content of those tables they actually need. I thought the easiest way to do this is to set a cookie value for each of those tables. So under the table I'd have a "add to favourites" button. This in theory should write a cookie. The "My favourites" page would simply display the divs (assets) that are written to the cookies.

 

I've got as far as the code below, but it doesn't seem to write my cookie:

BUTTON:
<div id="addToFav1" style="cursor:pointer;height:30px;width:120px;background-color:blue;">Add to favourites</div>

BUTTON CODE:
<script type=“text/javascript”>

$("#addToFav1").click(function () {
    $.cookie(‘Fav1’, ‘57734’, { expires: 120 });
});
    
</script>


(Talk) #2

Hey mate, it looks like you’re using the jquery cookie plugin. Have you installed the library?

Also, you could consider using localStorage, that way you have heaps of room to play with and don’t need to depend on a library.


You could:


Name each button item_%asset_assetid%, give it the class favourite


And:


$(’.favourite’).click(function(){

var thisID = $(this).attr(‘id’).replace(‘item_’,’’);

var faves = localStorage.getItem(‘favourites’);

if(! faves){

localStorage.setItem(‘favourites’, thisID);

} else {

localStorage.setItem(‘favourites’, faves + ‘,’ + thisID)

}


Then you could add a script to load them using a search asset with a blank design, searching for asset_ids, and accepting ANY words, like:


$(document).ready(function(){

var assets = localStorage.getItem(‘favourites’);

if(! assets){

} else {

$(’#favourites_div’).load(‘path-to-search?queries_assetids_query=’ + assets);

}

});


(Nic Hubbard) #3

If you want to purely use Matrix, which in this case I think is a great idea, you can easily create a trigger that will link a page under the current user. That way, you can late list all of the "favorites" that are linked by that user.

 

Here is the setup:

 

 

Then just create a link with a specific class and use some jQuery:

 

$('.addFavorites').click(function() {
   
    $.ajax({
          type: "POST",
          url: '%asset_url%?add_favorites=true',
          success: function() {
            alert('Added to favorites!');
        }
    });
        
});

This feels like a better solution as some users block cookies, or might clear them and wonder why their favorites are gone.


(Talk) #4

Are the registered logged in users or public users? If the former, then definitely store the data in Matrix.

Hey Nic I’ve used the link under user method before, but at the time I wondered whether if used in excess it would create a complex web of a lot of links, and if any changes cascade anywhere settings could be difficult to preserve, or something could accidentally have its status changed.


I recently tried a solution for registered users where if they favourite/follow a page, their user assetid is added to a private metadata field for the page containing an array of the asset ids of users who’ve favourited the page. This solution meant I could use triggers to send emails to the arrays, no strain was placed on the system, and if a page was moved or deleted, not much else happened. The other cool thing was I could use a regex match keyword on the layout to display whether the current user had favourited the page.


So Wardy, are your users registered and logged in?


(Nic Hubbard) #5

Hey Nic I've used the link under user method before, but at the time I wondered whether if used in excess it would create a complex web of a lot of links, and if any changes cascade anywhere settings could be difficult to preserve, or something could accidentally have its status changed.

 

It shouldn't cause any issues at all that I can think of. I think what you described would be a non-issue IMO. :)


(Talk) #6

Excellent. I feel much better about using the trigger/link setup for my previous employer now! Hey Wardie if you do use the trigger/link method, just make sure you don’t cascade any changes you make to users or groups that you wouldn’t want to apply to pages linked beneath them.


(Nic Hubbard) #7

Excellent. I feel much better about using the trigger/link setup for my previous employer now! Hey Wardie if you do use the trigger/link method, just make sure you don't cascade any changes you make to users or groups that you wouldn't want to apply to pages linked beneath them.

 

Alternately you could create a NOTICE link instead, which I don't think would be affected if a status change was cascaded. Although I have not tested this.


(Pomster09) #8

Thanks guys, yeah my solution would be for public users, so I need to use a method that utilises the client side. I'll press ahead with the cookies method, then I'll try the localStorage one.


(Pomster09) #9

I got the set cookie code working fine, it wasn't really far away.

$(function(){
      $("#addToFav1").click(function() {
           jQuery.cookie('Fav1', '57734');
           alert('You have set the cookie: '+ jQuery.cookie('Fav1'));
       });
});

(Fiona) #10
I recently tried a solution for registered users where if they favourite/follow a page, their user assetid is added to a private metadata field for the page containing an array of the asset ids of users who've favourited the page. This solution meant I could use triggers to send emails to the arrays, no strain was placed on the system, and if a page was moved or deleted, not much else happened. The other cool thing was I could use a regex match keyword on the layout to display whether the current user had favourited the page.

 

 

Jhewitt - I am trying to create this exact same functionality.. how do you go about creating the array of asset ids in the one metadata field? I have tried using a trigger to set a multiple text metadata value with the current user but it over-rides the value rather than adding a new value to it, so there is only ever one user assetid as the value at any one time...  any pointers appreciated, thx,


(Anthony Ponomarenko) #11

You can use a keyword to access the current metadata and append to that by adding a semi-colon and space between the two values eg.

 

'%asset_metadata_fieldName%; value'