Laravel + Ajax函数 - 更新“激活”状态的视图

时间:2015-02-24 08:55:53

标签: php ajax laravel

我刚刚开始使用ajax,现在我有点卡住了。我有一个ajax功能' GET'我需要在视图中显示所有值,并将其格式化为html。 我现在更新视图中显示的新闻的方式是在我的视图中调用脚本并让它有一个间隔(1000毫秒)。

现在我想弄清楚当我的控制器激活新闻帖子(当帖子从0 - > 1切换)时如何只运行ajaxGetAllActiveNews()。如果没有帖子被激活,我想要一个默认的'新闻'显示。

我对所有解决方案持开放态度!谢谢!

观点:

 <!-- News Activator -->
    <script>
        setInterval(function () {
            ajaxGetAllActiveNews();
        }, 1000);
    </script>

Ajax功能:

function ajaxGetAllActiveNews() {
    $.ajax({
        url: '/api/infoskjerm',
        method: 'GET'
    }).error(function (msg) {
        console.log(msg);
    }).done(function (news) {
        console.log(news);

        var titleToAppendToView = [];

        $(news).each(function (key, value) {
            titleToAppendToView.push('<td>' + value.title + '</td>');

        });

        $('#title').html(titleToAppendToView);


        var authorToAppendToView = [];

        $(news).each(function (key, value) {
            authorToAppendToView.push('<td>' + value.author + '</td>');

        });

        $('#author').html(authorToAppendToView);


        var messageToAppendToView = [];

        $(news).each(function (key, value) {
            messageToAppendToView.push('<td>' + value.message + '</td>');

        });

        $('#message').html(messageToAppendToView)


        var picturePathToAppendToView = [];

        $(news).each(function (key, value) {


            if (typeof value.picture_path !== 'undefined' && value.picture_path.length > 0) {
                picturePathToAppendToView.push('<img src="../uploads/' + value.picture_path + '"> </img>');
            }

        });

        $('#picture').html(picturePathToAppendToView);

    });
}

我的ajax和主动新闻控制器:

public function toggleActive($id)
{
    $news = News::findOrFail($id);
    $input = Input::except(['_token']);

        if($news->active == 0) {
            $news->active = 1;
            $news->update($input);
        }
        else {
            $news->active = 0;
            $news->update($input);
        }
    return Redirect::to('adminpanel/newsmodule');
}


 //Skriver ut alle active nyheter til infoskjerm view
public function ajaxGetAllActiveNews()
{
    $news = News::where('active', '=', '1')->get();
    return Response::json($news);
}

2 个答案:

答案 0 :(得分:0)

您可以使用WebSocket(http://socketo.me/)。这将允许您通过ZMQ(http://zeromq.org/bindings:php)进行推拉。对于客户端处理,您可能需要高速公路(http://autobahn.ws/)。

答案 1 :(得分:0)

经过一番搜索后,我将代码改写为此。代码现在从数据库中获取所有活动新闻,并将其限制为更新的3个最新帖子。然后我的ajax函数接收所有帖子并在我的视图中显示它们。

我的控制器:

public function ajaxGetAllActiveNews()
{
    $news = News::where('active', '=', '1')->orderBy('created_at', 'desc')->limit(3)->get();
    return Response::json($news);
}

我的Ajax:

function ajaxGetAllActiveNews() {
    $.ajax({
        url: '/api/infoskjerm',
        method: 'GET'
    }).error(function (msg) {
        console.log(msg);
    }).done(function (news) {
        console.log(news);

        var titleToAppendToView = [];

        $(news).each(function (key, value) {

            if (typeof value.picture_path !== 'undefined' && value.picture_path.length > 0)
            {
                var imgpath = '<img id="news-picture" width="100%" src="/uploads/' + value.picture_path + '"></img>';
            }
            else
            {
                imgpath = '';
            }

            titleToAppendToView.push('<div><h2>' + value.title + '</h2><h3>Utgiver: ' + value.author + '</h3><h4> Publisert: '
                                    + value.created_at + '</h4><p>' + value.message + '</p>' + imgpath + '</div>');

            $('#news-content').html(titleToAppendToView).append();
        });
    });
}