Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:40:35+00:00 2026-06-12T13:40:35+00:00

In my rails application, I have a piece of javascript that is exactly duplicate

  • 0

In my rails application, I have a piece of javascript that is exactly duplicate between 2 of my 8 views. Where is the proper place the place the javascript?

The rails application structure places javascript in app/assets/javascripts where it has a js.coffee file for each model and a application.js file. Do I place it in the application.js file or is there a way clean way to share javascript between two .js directories?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:40:37+00:00Added an answer on June 12, 2026 at 1:40 pm

    UPDATE:

    I totally misread this question and thought it was about backbone.js views, not rails views. The answer below is really something different than what was asked… maybe it will be relevant to people using backbone.js, but it doesn’t really answer this question. Sorry about that!

    ORIGINAL ANSWER:

    What I do to share code is to create a parent class, include the code I want to share in there and then have each of the views that use it extend that class (in coffeescript lingo).

    So something like (again in coffeescript):

    app/assets/javascripts/base_view.js.coffee (or wherever you want to put it)

    class App.BaseView
      sharedFunction: () ->
        ...
    

    and then make the other views extend App.BaseView (or whatever you call the parent class):

    app/assets/javascripts/views/view2.js.coffee

    class App.MyView1 extends App.BaseView
      ... sharedFunction() ...
    

    app/assets/javascripts/views/view2.js.coffee

    class App.MyView2 extends App.BaseView
      ... sharedFunction() ...
    

    Just make sure that the file with App.BaseView is loaded before the other views in application.rb. (If you’re using require.js then the load order won’t matter of course, but I’m assuming you’re not.)

    Also as a note, although you mention that you’re only sharing “a piece of javascript”, it’s really better to think from the start in terms of shared modules, so that if later you want to extend that “piece of javascript” you have the framework to do it. Here’s a good article on implementing modules with backbone.js.

    FWIW, this is the project I’m working on where I share code with common view classes: App.Threads extends App.TranslatableFieldsView which extends App.BaseView. Notice here I’m sharing initialize code using coffeescript’s super. I do the same thing for models.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails application I have a rake task that reads emails using imap.
I am building a Rails application that contains Developer s who have Application s.
I have a Rails application that accepts file uploads of CSV files. When developing
I have an rails a application whereby it has a page that contains a
In my rails 3 application I have added the stylesheets in /app/assets/stylesheets/ instead of
I'm developing a small application in Ruby-On-Rails. In a controller I have this piece
In a Rails application I have a Test::Unit functional test that's failing, but the
I have rails application that calculates prices of goods and I set prices in
In my rails application I have two trees that are identical but mounted in
I have serious problem with Thinking Sphinx in my Rails application. My app specifications

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.