eZ Community » Blogs » Thiago Campos Viana » Tip: Javascript and eZ publish...

By

Tip: Javascript and eZ publish communication using user preferences

Friday 19 August 2011 12:33:28 am

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

In this post I will show an easy way to make eZ publish communicate with javascript.

 

{set-block scope=global variable=cache_ttl}0{/set-block}
 
<input type="checkbox" id="var_test" name="var_test"
{if eq(ezpreference('var_test'),'true')} checked="checked"{/if}> Test
 
 
<script type="text/javascript">
var prefs_url="{"/user/preferences/set/var_test"|ezurl(no)}";
{literal}
$("#var_test").change(function() {
  $.ajax({
      url: (prefs_url+"/"+$("#var_test").is(":checked")),
      success: function(){}
    });
});
{/literal}
 
</script>

So, basically in this sample every time the user sets the checkbox value, he sets his user preference using an ajax call to the user/preferences view telling it to set the var value, so when the user reloads the page the checkbox will remain with its last state by getting the preference value using the ezpreference template operator. Simple right?

Proudly Developed with from