If I missed an easy part of whole thing forgive.
I am observing a strange behavior of Laravel Notifications between my local development and my server on Bluehost. On my PC it runs version 5.5 and Bluehost 5.4
I set the database notification and successfully can get them as expected.
<ul class="dropdown-menu list-unstyled msg_list" role="menu" onclick="markPostAsRead({{count(auth()->user()->unreadNotifications)}})">
<li>
#forelse (auth()->user()->unreadNotifications as $notification)
#include('layouts.notifications.'.snake_case(class_basename($notification->type)))
#empty
<strong>No notification</strong>
#endforelse
</li>
</ul>
Then when the user click it triggers a javascript to get the route to update the read_at column in my notification table in the database.
<script type="text/javascript" >
function markPostAsRead(countofNotification){
if(countofNotification !== '0')
$.get('/markasread');
}
</script>
Following is the route
Route::get('/markasread',function(){
auth()->user()->unreadNotifications->first()->markAsRead();
});
Everything went as expected on my local environment.
Once uploaded with all migrations in place, the notifications are shown as expected. However when the user clicks the notification the table does not update.
I checked that the js is working with some alerts on both server and local environment. But I do not understand why the table does not update on my server side.
If you could give me one hint it will really help me.
The notification html is a follows
<div class="top_nav">
<div class="nav_menu">
<nav>
<div class="nav toggle">
<a id="menu_toggle"><i class="fa fa-bars"></i></a>
</div>
<ul class="nav navbar-nav navbar-right">
<li role="presentation" class="dropdown" >
<a href="javascript:;" class="dropdown-toggle info-number" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-globe"></i>
<span class="badge bg-green">{{count(auth()->user()->unreadNotifications)}}</span>
</a>
<ul class="dropdown-menu list-unstyled msg_list" id="notification" role="menu" onclick="markPostAsRead({{count(auth()->user()->unreadNotifications)}})">
<li>
#forelse (auth()->user()->unreadNotifications as $notification)
#include('layouts.notifications.'.snake_case(class_basename($notification->type)))
#empty
<strong>No Unread Notification</strong>
#endforelse
</li>
</ul>
</li>.
</ul>
</nav>
</div>
</div>
<script type="text/javascript" >
var mynotificationcount;
function markPostAsRead(countofNotification){
mynotificationcount= countofNotification;
}
$('#notification').on('click', function(e){
e.preventDefault();
var href = this.href;
if(mynotificationcount !== '0') {
$.get('/markasread');
} else {
window.location.href = href;
}
});
</script>
Thanks,
The redirection of the page is faster than the Ajax request to update the table.
you should make the click event trigger the ajax.
<script type="text/javascript" >
$('#notification a').on('click', function(e){
if(countofNotification !== '0') {
$.get('/markasread');
}
});
</script>
if that doesnt work, you will need something more complicated like
<script type="text/javascript" >
$('#notification a').on('click', function(e){
e.preventDefault();
var href = this.href;
if(countofNotification !== '0') {
$.get('/markasread')->always(function(){window.location.href = href;});
} else {
window.location.href = href;
}
});
</script>
#N69S
Thank you very much.
I updated the java script a little bit to get around. I named the route and set as follows
<script type="text/javascript" >
var mynotificationcount;
function markPostAsRead(countofNotification){
mynotificationcount= countofNotification;
}
</script>
<script type="text/javascript" >
$('#notification a').on('click', function(e){
e.preventDefault();
var href = this.href;
var url = "{{route('markasread')}}";
if(mynotificationcount !== '0') {
$.get(url).always(function(){window.location.href = href;});
// window.location.href="{{ route('markasread') }}";
} else {
window.location.href = href;
}
});
</script>
Related
this is my first question on stack,i am not good at English,sorry for that.
my code can't stop jumping when i click the div.
list is a div contains some details.
my js:
function clickList(e)
{
var listAll=e.parentNode.childNodes;
var list=listAll[1];
console.log(list);
if (list.style.display == "none")
{
list.style.display = "block";
}
else
{
list.style.display = "none";
}
e.stopPropagation();
}
my html:
<a href="an address">
<div class="pull-right" onclick="clickList(this)">
<i class="iconfont">๎</i>
</div>
</a>
As you are stopping the event propagation, you can just have javascript:void() as below in href instead of address:
<a href="javascript:void(0);">
<div class="pull-right" onclick="clickList(this)">
<i class="iconfont">๎</i>
</div>
</a>
Also Remove e.stopPropagation(); in clickList as this will be the div object and not an event object.
From a controller a do a ajax call. The call can be triggered from two different links, both in the same html template. When using the above link that switches to the tab the data returned by the ajax call is displayed correctly. When using the refresh link the element in the ng-repeat won't be updated. Anyone knows why this is?
angularjs:
app.controller("ActiveSimulations",
["$scope", "$http", "socket",
function($scope, $http, socket){
$scope.active_simulations_dict = {};
$scope.get_active_simulations = function() {
var responsePromise = $http.get("/active_simulations");
responsePromise.success(function (data, status, headers, config) {
$scope.active_simulations_dict = data;
console.log($scope.active_simulations_dict)
});
responsePromise.error(function (data, status, headers, config) {
console.log('Warning - "AJAX failed!"')
alert("AJAX failed!");
});
};
}]);
HTML
<div id="followSim" class="tab-pane fade">
<h3>Follow running simulations</h3>
<div class="inline-containers">
<div id="simulation-list-wrapper">
<ul class="list-unstyled">
<li ng-repeat="(key, value) in active_simulations_dict">
<a ng-show="value[0] && value[2]" ng-click="followActiveSimulation(key)">
{[{ value[0] }]} ( {[{ value[2] }]} is director)
</a>
</li>
</ul>
</div> <!--end simulation list wrapper-->
<div id="refresh-simulation-list" ng-controller="ActiveSimulations">
<a id="refresh-link"
class="right-side"
ng-click="get_active_simulations()">
<i class="icon-refresh right-side"></i>
refresh list
</a>
</div>
As #kasoban pointed out you might have an issue with multiple instances. Best practice is to create a service which handles your requests. You can inject it and call the request function from any controller then e.g.:
$scope.active_simulations_dict = RequestService.get_active_simulations();
Source code is like this:
<div>
<h4>comment content</h4>
<a id="delcmt_{{ comment.id }}">delete this comment</a>
</div>
......
<div>
<h4>comment content</h4>
<a id="delcmt_{{ comment.id }}">delete this comment</a>
</div>
I what to add ajax function to each of the "delete this comment" link:
<script type=text/javascript>
$(function() {
$('a#delcmt_id').bind('click', function() {
$.get($SCRIPT_ROOT + '/del_comment', {
}, function(data) {
$("#result").value(data.result);
});
return false;
});
});
</script>
What I can come out is using a loop to copy the upper ajax function for each comment, that must be very ugly. Any good ideas?
Try adding a class and select it with jquery add an event handler. You have to use the 'on' event because the elements you wish attach behavior to might be dynamic and load after document ready.
#*Render all this with razor, or angular or knockout*#
<div>
<h4>comment content</h4>
<span style="cursor: pointer;" id="1" data-rowid="1" class="delete-me-class">delete this comment</span>
</div>
<div>
<h4>comment content</h4>
<span style="cursor: pointer;" id="2" data-rowid="2" class="delete-me-class">delete this comment</span>
</div>
<script>
$(function () {
$('body').on('click', '.delete-me-class', function () {//http://api.jquery.com/on/ on is the latest 'live' binding for elements that may not exists when DOM is ready.
var rowId = $(this).data('rowid');
//TODO Use rowId for your delete ajax, or your element Id if you wish.
alert('You clicked on the delete link with the row ID of ' + rowId);
});
});
</script>
Here is a working Fiddle
My html
<ul class="nav nav-tabs tabs-up" id="friends">
<li> Contacts </li>
<li> Friends list</li>
<li>Awaiting request</li>
</ul>
<div class="tab-pane active" id="contacts">
</div>
<div class="tab-pane" id="friends_list">
</div>
<div class="tab-pane urlbox span8" id="awaiting_request">
</div>
My javascript code :
$('[data-toggle="tabajax"]').click(function (e)
{
e.preventDefault()
var loadurl = $(this).attr('href')
var targ = $(this).attr('data-target')
$.get(loadurl, function(data) {
$(targ).html(data)
});
$(this).tab('show')
return false;
});
I have given links for tabs respectively. But i couldnt render those links in the page when a tab is clicked..
Could anyone please help me out of this.
Seems to work fine for me:
JSFiddle
The only code that I added to yours (aside from using some gists for the ajax calls) was putting the tab panes inside a <div class="tab-content">.
I'm working on something where there are 2 links which triggers some Ajax. However I need to turn the links into Flash buttons (AS3). I've never worked with Ajax before, and I have no idea how this can be done.
Edit:
The Ajax:
<script>
$(document).ready(function() {
$('a.catlink').unbind('click').click(function(e)
{
e.preventDefault();
var link = $(this);
var inputs = [];
var cat_type = $(this).attr('href').replace('#', '');
link.attr('disabled', 'disabled');
inputs.push('cat_type=' + escape(cat_type));
$.ajax(
{
type: "POST",
cache: false,
dataType: "html",
url: window.location.href,
data: inputs.join('&'),
success: function(html)
{
var json = $.parseJSON(html);
if (json['status'] == 1)
{
$('div.listing').html(json['html']);
}
link.removeAttr('disabled');
}
});
});
});
</script>
The HTML
<h1 class="section-head">Products
// **The 2 links*** //
<a class="catlink" href="#cinema">cinema</a> <a class="catlink" href="#smart">smart</a></h1>
<div class="listing">
<ul class="listing">
{foreach from=$products item="product_info"}
<li class="clearfix">
<div class="inner-left">
<img height="68" width="90" src="{$product_info.image}" />
<h2 class="normal mt-5">{if $product_info.price != '0'}${$product_info.price}{else}Click for price ยป{/if}</h2>
</div>
<div class="inner-right">
<h3 class="mb-0">{$product_info.productName}</h3>
<p class="small mb-5"><span class="quiet">{$product_info.category}</span></p>
<p class="mb-15">{$product_info.description}</p>
<a class="button getprice" href="{$product_info.url}">Buy Now</a>
<br><br>
</div>
</li>
{/foreach}
</ul>
</div>
If you're going to use AS3 then you can use ExternalInterface.call() to call a JavaScript function on the page. Though, using Ajax may not be required if you're using AS3 because you can make use of the URLLoader class to do the same thing (call a PHP script and decode a result).
If you describe more accurately what you want to achieve then I shall provide some example code and clarify a little more.