nathansmith / adapt

Adapt.js serves CSS based on screen width.

Home Page:http://adapt.960.gs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

You cannot not define the path

rhulse opened this issue · comments

I am using this in Rails and am using helper methods to get the full path to the CSS files. When I comment out the path definition in the CONFIG the script no longer inserts the stylesheet links.

When path is not set in the config it is set to an empty string on line 26.

Was it your intention to not insert links if path is not explicitly set?

Correct.

If path is undefined, then it just uses an empty string…

https://github.com/nathansmith/adapt/blob/master/assets/js/adapt.js#L26

Then, on line 105 it first checks for the existence of the path before it inserts anything…

https://github.com/nathansmith/adapt/blob/master/assets/js/adapt.js#L105

I'm curious how would you be using Rails to pass the full path to JS?

Assuming it's something you could emit to the page, could you do something like…

<script>
  var ADAPT_CONFIG = {
    path: '<%= ... %>',
    dynamic: true,
    range: [...]
  };
</script>

The rails helpers give the whole path to the asset, from root, so you don't need a global path to assemble things.

My config:

var ADAPT_CONFIG = {
  // Where is your CSS?
  //path: '/',

  // false = Only run once, when page first loads.
  // true = Change on window resize and page tilt.
  dynamic: true,

  // Optional callback... myCallback(i, width)
  //callback: myCallback,

  // First range entry is the minimum.
  // Last range entry is the maximum.
  // Separate ranges by "to" keyword.
  range: [
    '0px    to 600px  = <%= stylesheet_path('adaptive/600') %>',
    '601px  to 767px  = <%= stylesheet_path('adaptive/768') %>',
    '768px  to 960px  = <%= stylesheet_path('adaptive/960') %>',
    '961px            = <%= stylesheet_path('adaptive/inf') %>'
  ]
};

And in Adapt.js I've done this:

if (path != undefined) {
  (d.head || d.getElementsByTagName('head')[0]).appendChild(css);
}

Why the check for path != undefined then?

If you're passing the string of "/" it should work fine.

Rails already includes '/' at the front of all paths it generates.

Ah, okay. Can you give me an example of what you'd like the config to be?

I'll try to see if there's a way to facilitate that, without breaking anything for others.

I'd like to be able to use an empty string for path, as Rails generates the complete path.

The current behaviour is that if no path is set it will not append any links to the style sheet because path = "" is falseish. That seems reasonable.

(In retrospect, my local patch is nonsense - path is never undefined; I could have just removed it from the expression.)

Would removing 'path &&' be an option? What would it break?

Hmm, I think doing this would work.

Change this:

path && (d.head || d.getElementsByTagName('head')[0]).appendChild(css);

To this:

url && (d.head || d.getElementsByTagName('head')[0]).appendChild(css);

Because, what we really care about is whether there's a URL that's been constructed (path + file), not the existence of the path itself.

And, patched…

f322c18

Let me know if for some reason that doesn't work, but I think it should be good to go.

Yes! That looks good. You might want to update the comment too. :-)

https://github.com/nathansmith/adapt/blob/master/assets/js/adapt.js#L103

Good call. Done.