0

I like to keep my javascript out of views if possible, but I've found that difficult in certain situations.

Say I have a video partial that has corresponding javascript (using jplayer):

_video.haml

.video
  content_for :javascript/head
    / jplayer instance code here

One alternative I've thought is to hook into .video from within a separate js file and instantiate jplayer objects via that.

videos.coffee

$('.video').each
  # jplayer instance code here

My question is, what about pages where '.video' isn't found? Will it cause problems if I have lots of jquery finders that are missing?

1 Answer 1

1

you could try to check the length of .video and only run if it does exist

var $video = $('.video');
if(video.length !== 0){
     $('.video').each
     # jplayer instance code here
}

it checks the length of that jQuery object in the DOM

Sign up to request clarification or add additional context in comments.

1 Comment

@Zenph did this answer your question? If i answered can you please mark it as answered :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.