codebrew / backbone-rails

Easily use backbone.js with rails 3.1
MIT License
1.61k stars 255 forks source link

Using backbone-rails with jasmine-webkit-headless #97

Closed krikis closed 12 years ago

krikis commented 12 years ago

When I tried to run jasmine-webkit-headless it complained that it could not find the backbone/#{application_name} file.

couldn't find file 'backbone/application_name'

This refers to an entry in 'application.js' placed there to load the generated/scaffolded main backbone application file into the Rails assets pipeline:

//= require backbone/application_name

When this line is changed to

//= require ./backbone/application_name

the problem is solved. In other application.js files I saw relative paths being loaded using the leading dot-slash too.

Maybe you could adapt your generators/scaffolders to incorporate this minor change.

ryanfitz commented 12 years ago

when you load up the application as usual, outside of jasmine-webkit, do you get the same errors? Also what version of rails are you using?

krikis commented 12 years ago

I'm using rails 3.2. The issue only occurs when running jasmine tests.

Cheers

krikis commented 12 years ago

I took this from the Sprockets 2.1.2 source code:

  # The `require` directive functions similar to Ruby's own `require`.
  # It provides a way to declare a dependency on a file in your path
  # and ensures its only loaded once before the source file.
  #
  # `require` works with files in the environment path:
  #
  #     //= require "foo.js"
  #
  # Extensions are optional. If your source file is ".js", it
  # assumes you are requiring another ".js".
  #
  #     //= require "foo"
  #
  # Relative paths work too. Use a leading `./` to denote a relative
  # path:
  #
  #     //= require "./bar"
  #
  def process_require_directive(path)
    if @compat
      if path =~ /<([^>]+)>/
        path = $1
      else
        path = "./#{path}" unless relative?(path)
      end
    end

    context.require_asset(path)
  end

The comments instruct to use a leading "./" for relative paths. The code however prepends this "./" when the path is not relative. That seems to be why relative assets can be required even when the path is not defined as a relative path.

Apparently the jasmine-rails gem (https://github.com/searls/jasmine-rails) does not have this behavior and therefore cannot require the relative files.

This only applies when compatibility mode is enabled. Still the jasmine-rails' require_jasmine implementation does something awkwardly different to the sprockets require, which make loading relative assets fail.

krikis commented 12 years ago

Since this problem seems to be more related to the jasmine-rails gem, I decided to stop using it. Now everything works fine when compiling assets.