Как я делаю сейчас, чтобы обновить новости, показанные в представлении, я обращаюсь к сценарию, и даю, чтобы он был проведен (1000 мс). активируется (когда мой контроллер переключен от 0 -> 1). И если посты не активированы, я хочу показать «новости» по умолчанию. < /P>
Я открыт для всех решений! Спасибо! < /P>
View: < /p>
setInterval(function () {
ajaxGetAllActiveNews();
}, 1000);
< /code>
ajax function: < /p>
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('' + value.title + '');
});
$('#title').html(titleToAppendToView);
var authorToAppendToView = [];
$(news).each(function (key, value) {
authorToAppendToView.push('' + value.author + '');
});
$('#author').html(authorToAppendToView);
var messageToAppendToView = [];
$(news).each(function (key, value) {
messageToAppendToView.push('' + value.message + '');
});
$('#message').html(messageToAppendToView)
var picturePathToAppendToView = [];
$(news).each(function (key, value) {
if (typeof value.picture_path !== 'undefined' && value.picture_path.length > 0) {
picturePathToAppendToView.push('
');
}
});
$('#picture').html(picturePathToAppendToView);
});
}
< /code>
Мой контроллер для Ajax и Active News: < /p>
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);
}
Подробнее здесь: https://stackoverflow.com/questions/286 ... ated-state
Мобильная версия