Problem when following links (In-app Notifications with JavaScript)
So I followed along the Gorails tutorial on how to an in-app notifications feature (notifications bell). It works, but there seems to be a problem when following the links on the notifications dropdpwn (actually following all the links in the app). When following a link the notifications feature doesn't work anymore :( Only on refreshing the page!
Any ideas on solving this issue?
app/assests/javascripts/notifications.coffee
class Notifications
constructor: ->
@notifications = $("[data-behavior='notifications']")
if @notifications.length > 0
@handleSuccess @notifications.data("notifications")
$("[data-behavior='notifications-link']").on "click", @handleClick
setInterval (=>
@getNewNotifications()
), 5000
getNewNotifications: ->
$.ajax(
url: "/notifications.json"
dataType: "JSON"
method: "GET"
success: @handleSuccess
)
handleClick: (e) =>
$.ajax(
url: "/notifications/mark_as_read"
dataType: "JSON"
method: "POST"
success: ->
$("[data-behavior='unread-count']").text(0)
)
handleSuccess: (data) =>
items = $.map data, (notification) ->
notification.template
unread_count = 0
$.each data, (i, notification) ->
if notification.unread
unread_count += 1
$("[data-behavior='unread-count']").text(unread_count)
$("[data-behavior='notification-items']").html(items)
jQuery ->
new Notifications
app/views/layouts/application.html.haml (Bulma)
!!!
%html
%head
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%title 5igma
= csrf_meta_tags
%meta{:content => "width=device-width, initial-scale=1", :name => "viewport"}/
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
%body
- if flash[:notice]
.notification.is-success.global-notification
%p.notice= notice
- if flash[:alert]
.notification.is-danger.global-notification
%p.alert= alert
= render 'layouts/navbar'
= yield
app/views/layouts/_navbar.html.haml
%nav.navbar.is-link{"aria-label" => "main navigation", :role => "navigation"}
.navbar-brand
= link_to root_path, class:"navbar-item" do
%h1.title.is-5.has-text-white 5igma
.navbar-burger.burger{"data-target" => "navbar"}
%span
%span
%span
#navbar.navbar-menu
.navbar-end
- if user_signed_in?
.navbar-end
.navbar-item.has-dropdown.is-arrowless{ 'data-behavior': 'notifications', "data-notifications" => "#{render template: "notifications/index", formats: [:json]}" }
%a.navbar-link#dropdown{ 'data-behavior': 'notifications-link' }
%icon.fa.fa-bell
%span.tag.is-rounded.is-small.ml1{ 'data-behavior': 'unread-count' }
.navbar-dropdown#notifications{ 'data-behavior': 'notification-items' }
app/views/notifications/index.json.jbuilder
json.array! @notifications do |notification|
json.id notification.id
json.unread !notification.read_at?
json.template render partial: "notifications/#{notification.notifiable_type.underscore.pluralize}/#{notification.action}", locals: {notification: notification}, formats: [:html]
end
javascript ruby-on-rails notifications bulma
add a comment |
So I followed along the Gorails tutorial on how to an in-app notifications feature (notifications bell). It works, but there seems to be a problem when following the links on the notifications dropdpwn (actually following all the links in the app). When following a link the notifications feature doesn't work anymore :( Only on refreshing the page!
Any ideas on solving this issue?
app/assests/javascripts/notifications.coffee
class Notifications
constructor: ->
@notifications = $("[data-behavior='notifications']")
if @notifications.length > 0
@handleSuccess @notifications.data("notifications")
$("[data-behavior='notifications-link']").on "click", @handleClick
setInterval (=>
@getNewNotifications()
), 5000
getNewNotifications: ->
$.ajax(
url: "/notifications.json"
dataType: "JSON"
method: "GET"
success: @handleSuccess
)
handleClick: (e) =>
$.ajax(
url: "/notifications/mark_as_read"
dataType: "JSON"
method: "POST"
success: ->
$("[data-behavior='unread-count']").text(0)
)
handleSuccess: (data) =>
items = $.map data, (notification) ->
notification.template
unread_count = 0
$.each data, (i, notification) ->
if notification.unread
unread_count += 1
$("[data-behavior='unread-count']").text(unread_count)
$("[data-behavior='notification-items']").html(items)
jQuery ->
new Notifications
app/views/layouts/application.html.haml (Bulma)
!!!
%html
%head
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%title 5igma
= csrf_meta_tags
%meta{:content => "width=device-width, initial-scale=1", :name => "viewport"}/
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
%body
- if flash[:notice]
.notification.is-success.global-notification
%p.notice= notice
- if flash[:alert]
.notification.is-danger.global-notification
%p.alert= alert
= render 'layouts/navbar'
= yield
app/views/layouts/_navbar.html.haml
%nav.navbar.is-link{"aria-label" => "main navigation", :role => "navigation"}
.navbar-brand
= link_to root_path, class:"navbar-item" do
%h1.title.is-5.has-text-white 5igma
.navbar-burger.burger{"data-target" => "navbar"}
%span
%span
%span
#navbar.navbar-menu
.navbar-end
- if user_signed_in?
.navbar-end
.navbar-item.has-dropdown.is-arrowless{ 'data-behavior': 'notifications', "data-notifications" => "#{render template: "notifications/index", formats: [:json]}" }
%a.navbar-link#dropdown{ 'data-behavior': 'notifications-link' }
%icon.fa.fa-bell
%span.tag.is-rounded.is-small.ml1{ 'data-behavior': 'unread-count' }
.navbar-dropdown#notifications{ 'data-behavior': 'notification-items' }
app/views/notifications/index.json.jbuilder
json.array! @notifications do |notification|
json.id notification.id
json.unread !notification.read_at?
json.template render partial: "notifications/#{notification.notifiable_type.underscore.pluralize}/#{notification.action}", locals: {notification: notification}, formats: [:html]
end
javascript ruby-on-rails notifications bulma
add a comment |
So I followed along the Gorails tutorial on how to an in-app notifications feature (notifications bell). It works, but there seems to be a problem when following the links on the notifications dropdpwn (actually following all the links in the app). When following a link the notifications feature doesn't work anymore :( Only on refreshing the page!
Any ideas on solving this issue?
app/assests/javascripts/notifications.coffee
class Notifications
constructor: ->
@notifications = $("[data-behavior='notifications']")
if @notifications.length > 0
@handleSuccess @notifications.data("notifications")
$("[data-behavior='notifications-link']").on "click", @handleClick
setInterval (=>
@getNewNotifications()
), 5000
getNewNotifications: ->
$.ajax(
url: "/notifications.json"
dataType: "JSON"
method: "GET"
success: @handleSuccess
)
handleClick: (e) =>
$.ajax(
url: "/notifications/mark_as_read"
dataType: "JSON"
method: "POST"
success: ->
$("[data-behavior='unread-count']").text(0)
)
handleSuccess: (data) =>
items = $.map data, (notification) ->
notification.template
unread_count = 0
$.each data, (i, notification) ->
if notification.unread
unread_count += 1
$("[data-behavior='unread-count']").text(unread_count)
$("[data-behavior='notification-items']").html(items)
jQuery ->
new Notifications
app/views/layouts/application.html.haml (Bulma)
!!!
%html
%head
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%title 5igma
= csrf_meta_tags
%meta{:content => "width=device-width, initial-scale=1", :name => "viewport"}/
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
%body
- if flash[:notice]
.notification.is-success.global-notification
%p.notice= notice
- if flash[:alert]
.notification.is-danger.global-notification
%p.alert= alert
= render 'layouts/navbar'
= yield
app/views/layouts/_navbar.html.haml
%nav.navbar.is-link{"aria-label" => "main navigation", :role => "navigation"}
.navbar-brand
= link_to root_path, class:"navbar-item" do
%h1.title.is-5.has-text-white 5igma
.navbar-burger.burger{"data-target" => "navbar"}
%span
%span
%span
#navbar.navbar-menu
.navbar-end
- if user_signed_in?
.navbar-end
.navbar-item.has-dropdown.is-arrowless{ 'data-behavior': 'notifications', "data-notifications" => "#{render template: "notifications/index", formats: [:json]}" }
%a.navbar-link#dropdown{ 'data-behavior': 'notifications-link' }
%icon.fa.fa-bell
%span.tag.is-rounded.is-small.ml1{ 'data-behavior': 'unread-count' }
.navbar-dropdown#notifications{ 'data-behavior': 'notification-items' }
app/views/notifications/index.json.jbuilder
json.array! @notifications do |notification|
json.id notification.id
json.unread !notification.read_at?
json.template render partial: "notifications/#{notification.notifiable_type.underscore.pluralize}/#{notification.action}", locals: {notification: notification}, formats: [:html]
end
javascript ruby-on-rails notifications bulma
So I followed along the Gorails tutorial on how to an in-app notifications feature (notifications bell). It works, but there seems to be a problem when following the links on the notifications dropdpwn (actually following all the links in the app). When following a link the notifications feature doesn't work anymore :( Only on refreshing the page!
Any ideas on solving this issue?
app/assests/javascripts/notifications.coffee
class Notifications
constructor: ->
@notifications = $("[data-behavior='notifications']")
if @notifications.length > 0
@handleSuccess @notifications.data("notifications")
$("[data-behavior='notifications-link']").on "click", @handleClick
setInterval (=>
@getNewNotifications()
), 5000
getNewNotifications: ->
$.ajax(
url: "/notifications.json"
dataType: "JSON"
method: "GET"
success: @handleSuccess
)
handleClick: (e) =>
$.ajax(
url: "/notifications/mark_as_read"
dataType: "JSON"
method: "POST"
success: ->
$("[data-behavior='unread-count']").text(0)
)
handleSuccess: (data) =>
items = $.map data, (notification) ->
notification.template
unread_count = 0
$.each data, (i, notification) ->
if notification.unread
unread_count += 1
$("[data-behavior='unread-count']").text(unread_count)
$("[data-behavior='notification-items']").html(items)
jQuery ->
new Notifications
app/views/layouts/application.html.haml (Bulma)
!!!
%html
%head
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%title 5igma
= csrf_meta_tags
%meta{:content => "width=device-width, initial-scale=1", :name => "viewport"}/
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
%body
- if flash[:notice]
.notification.is-success.global-notification
%p.notice= notice
- if flash[:alert]
.notification.is-danger.global-notification
%p.alert= alert
= render 'layouts/navbar'
= yield
app/views/layouts/_navbar.html.haml
%nav.navbar.is-link{"aria-label" => "main navigation", :role => "navigation"}
.navbar-brand
= link_to root_path, class:"navbar-item" do
%h1.title.is-5.has-text-white 5igma
.navbar-burger.burger{"data-target" => "navbar"}
%span
%span
%span
#navbar.navbar-menu
.navbar-end
- if user_signed_in?
.navbar-end
.navbar-item.has-dropdown.is-arrowless{ 'data-behavior': 'notifications', "data-notifications" => "#{render template: "notifications/index", formats: [:json]}" }
%a.navbar-link#dropdown{ 'data-behavior': 'notifications-link' }
%icon.fa.fa-bell
%span.tag.is-rounded.is-small.ml1{ 'data-behavior': 'unread-count' }
.navbar-dropdown#notifications{ 'data-behavior': 'notification-items' }
app/views/notifications/index.json.jbuilder
json.array! @notifications do |notification|
json.id notification.id
json.unread !notification.read_at?
json.template render partial: "notifications/#{notification.notifiable_type.underscore.pluralize}/#{notification.action}", locals: {notification: notification}, formats: [:html]
end
javascript ruby-on-rails notifications bulma
javascript ruby-on-rails notifications bulma
edited Nov 22 '18 at 17:18
wanttobeprofessional
1,02931323
1,02931323
asked Nov 21 '18 at 16:01
Ariel WulkanAriel Wulkan
12
12
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415986%2fproblem-when-following-links-in-app-notifications-with-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415986%2fproblem-when-following-links-in-app-notifications-with-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown