Search code examples
javascriptruby-on-railsflickrgalleria

Javascript string interpolation


I have a Galleria (galleria.io) that works when the code looks like this:

Galleria.run('.galleria', {
  flickr: 'set:72157640607666844', }

But when I try to set the set number as a variable, it breaks with a "Fatal error: 1 fail: Photoset not found"

var fakeVar = 72157640607666844
  Galleria.run('.galleria', {
  flickr: 'set:' + fakeVar, }

Any thoughts on how to get this to work?

FYI: I looked at console.log('set:' + fakeVar), and it certainly is returning set:72157640607666844. Initially, I thought it was because it was returning the set without quotes, but I know this is not the case, because if I do " 'set:"+ fakeVar + " ' ", I get a no method error for 'set.

Also if it's helpful, this is the Galleria program code that defines the set method:

set: function( photoset_id, callback ) {
    return this._find({
        photoset_id: photoset_id,
        method: 'flickr.photosets.getPhotos'
    }, callback);
},

Solution

  • 72157640607666844 is too significant of a Number for JavaScript to store completely:

    console.log(72157640607666844);
    //          72157640607666850
    

    To avoid issues with precision, you can wrap the literal in quotes so it's a String instead:

    var fakeVar = '72157640607666844';