Why does evently suggest $$(this) to save state?

138 Views Asked by At

As explained here, to save state which must be accessible in different events, $$(this) is recommended, like this:

$$(this).filters = "myvalue";

What does that syntax mean? Why $$ (double dollar)? Why this? Why the () (parentheses)?

That code is not working for me anyway. I must use something like:

$.filters = "myvalue";

I would like to understand why the second form is working, and the first one not.

1

There are 1 best solutions below

0
On BEST ANSWER

Just check the source code of eventlly, $$ is just a shorthand for jQuery's data method:

function $$(node) {
  var data = $(node).data("$$");
  if (data) {
    return data;
  } else {
    data = {};
    $(node).data("$$", data);
    return data;
  }
};

so, in a nutshell - it calls data on whatever jQuery returns for your argument, if there is no data attached it creates empty object, use this as new data and returns it.

$.filters = "myvalue"

This works by assigning the value to global jQuery object, while $$ attaches your data to object represented by query.