跟随并停止关注与使用jQuery和放大器的单按钮; AJAX,通过laravel控制器 [英] follow and unfollow using single button with jquery & ajax, by laravel controller

查看:128
本文介绍了跟随并停止关注与使用jQuery和放大器的单按钮; AJAX,通过laravel控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器,用于跟踪并分别停止关注。我想设计一个按钮,将在网站上拥有后续的很多地方使用 - 取消关注 - 下面的操作。

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.

我指的是用户在下面将被宣布为以下,直到悬停效果调用否则它会说按照andwhen悬停来它给取消关注,如果下已经或继续后续的选项时。

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'); }} 

和取消关注

{{ action('FollowsController@destroy', 'id'); }}

在这里展现的是post方法和销毁是有删除操作。

here show is post method and destroy is having delete action.

我在这里提供的code中的全面行动:虽然这code没有给我想要的呢。

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.

推荐答案

部分发现了问题的解决方案。但是,还没有走很长的。 第一次执行这显示mehod控制器可以通过将perticular页面,该页面读取控制器本身....的链接来完成

The solution of the question partly discovered. But yet to go a long. the first execution which is show mehod in the controller can be done by going to the link of perticular page which fetch the controller itself ....

一样,如果 {{行动(FollowsController @灭','身份证')}} 获取这样的链接的http://本地主机/布拉布拉/ ID

like if {{ action('FollowsController@destroy', 'id') }} fetch a link like this http://localhost/blabla/id

接下来我们就来获取

 if( action == "follow" ) {
        myUrl = "http://localhost/blabla/"+id;
        myMethod = "GET";
    }

原因从JS控制器呼叫不能得到解决,因为它应该与JS变量获取的。

Cause the controller call from the js can not be resolved as it should be fetched with JS variable.

直到这是工作

但在案件的删除方法会是怎样的链接调用这仍然是一个问题。当我得到这个问题的答案我会尝试将它张贴在这里....谢谢!

But in case of delete method what will be the the link to call that is still a question. when I get that answer I will try to post it here.... thanks !

myUrl ={{行动(FollowsController @灭','身份证')}};

myUrl = "{{ action('FollowsController@destroy', 'id') }}" ;

这篇关于跟随并停止关注与使用jQuery和放大器的单按钮; AJAX,通过laravel控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆