Thursday, November 28, 2013

How to load the server page in mobile application using jquery mobile

My application requirements is to show the dynamic pages on the mobile application using the jquery mobile.

Here is my approach
1) bind the pagebeforechange event.
2) Call the javascript function inside the pagebeforechange function.
3) Inside the function call the ajax and insert the content in the page.
4) Onsuccessfull ajax request trigger the pagechange event.


Here is sample code:
1) $(document).on('pagebeforechange', function (e, data) {
      if (typeof data.toPage === "string") {
           if (u.hash.search(/^#action_url/) !== -1) {

                populate_content(u, data.options);

                e.preventDefault();
            }  
     }
});


2) function populate_content(u, options) {
  
    $.ajax({url: remote_location_url,
                success: function (result) {
                      insert content here ............
                }
              
            });

            $.mobile.changePage($('#page_change'), options);

        } 

}

Thursday, November 14, 2013

Cookies are not set in AJAX request

I am setting the cookies to browser by an .js ajax request. After some time when i am sending the .html extension ajax request  my browser is not sending cookies along with the request in ajax call. After some googling i found that i have to set the

xhrFields: {
           withCredentials: true
}

in my ajax call.

Thursday, November 7, 2013

Get the controller object in model rails

My Site require a audit trail functionality who did activity, what are changes he made, from which ip address and lot more details. So i written the after_create, after_update, and after_destroy ActiveRecord callback in the ActiveRecord::Base class. It solves my purpose but i need who did it, from which ip he did it.

So i added a before filter in application controller which add the controller instance to current thread,

 Thread.current.thread_variable_set('controller', self)

As in the same thread the operation happening on the ActiveRecord.

So i can get back the controller instance and call the current_user method on it

Thread.current.thread_variable_get('controller').current_user

now i have to make my current_user method public and to avoid it calling from browser as action i have to hide it by

hide_action :current_user