I am having two controller for follow and unfollow separately. I want to design a single button which will be used in many places of the website featuring follow - unfollow - following action.
I mean when the user is following it will be declared as following until the hover effect call else it will say follow andwhen hover come it gives unfollow if following already or continue the follow option.
That was the details of the button outlook. Now When I get into the implementation, I can do it for single controller call where the checking done , (by gettin some help from internet). But I want to call two different controller for two different action.
like for follow-> {{ action('FollowsController@show', 'id'); }}
and for unfollow
{{ action('FollowsController@destroy', 'id'); }}
here show is post method and destroy is having delete action.
here i am providing the full action in code : though this code is not giving what I want anyway.
<script type="text/javascript">
//Changes the Following text to Unfollow when mouseover the button
$(document).ready(function()
{
$('.following').hover(function()
{
$(this).text("Unfollow");
},function()
{
$(this).text("Following");
});
});
//Perform the Following and Unfollowing work
function follow_or_unfollow(id,action)
{
var dataString = "id=" + id;
$.ajax({
type: "POST",
url: "{{ action('FollowsController@show', 'id'); }}",
data: dataString,
beforeSend: function()
{
if ( action == "following" )
{
$("#following"+id).hide();
$("#loading"+id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
}
else if ( action == "follow" )
{
$("#follow"+id).hide();
$("#loading"+id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
}
else { }
},
success: function(response)
{
if ( action == "following" ){
$("#loading"+id).html('');
$("#follow"+id).show();
}
else if ( action == "follow" ){
$("#loading"+id).html('');
$("#following"+id).show();
}
else { }
}
});
}
</script>
@if(0)
<span id="loading {{ $topdebater->id }}"></span>
<button class="btn btn-lg btn-danger button following" id="following {{ $topdebater->id }}" onClick="follow_or_unfollow({{ $topdebater->id }},'following');">Following</button>
<span style="display:none;" class="btn btn-lg button follow" id="follow {{ $topdebater->id }} ?>" onClick="follow_or_unfollow({{ $topdebater->id }},'follow');">Follow</span>
@else
<span id="loading{{ $topdebater->id }}"></span>
<button class="btn btn-info button follow" id="follow{{ $topdebater->id }}" onClick="follow_or_unfollow({{ $topdebater->id }},'follow');">Follow</button>
<span style="display:none;" class="button following" id="followingfollow{{ $topdebater->id }}" onClick="follow_or_unfollow(follow{{ $topdebater->id }},'following');">Following</span>
@endif
I am very new at ajax. learning hard though. I need you support in this research . Thanks in advance.