appjudo / skim

Fat-free client-side templates with Slim and CoffeeScript
MIT License
273 stars 53 forks source link

Possible global scope issue? #56

Open nroose opened 8 years ago

nroose commented 8 years ago

I have one template inadvertently calling the Skim.withContext method of another template. It seems as though this should not happen, but it very definitely is. Any chance the call to Skim.withContext can be changed to be more specific? I have many templates and it's only happening with these two.

svicalifornia commented 8 years ago

@nroose Yes, the default behavior is to include the Skim preamble at the top of each compiled template. The preamble defines Skim as a global variable, and each template redefines this object, so all Skim templates will use the Skim object defined by the last template loaded.

Skim.withContext shouldn't rely on or alter any context specific to its enclosing template, so in theory it shouldn't matter which template defined it. Please let me know what side effects you're seeing so we can investigate further. (If you prefer, you can PM me via the email address on my GitHub profile.)

To avoid redefining the Skim preamble in each template, you can set the use_asset option in your Ruby code after loading the gem:

Skim::Engine.default_options[:use_asset] = true

Then you can add the Skim preamble manually to any JS file in a Sprockets asset pipeline:

//= require skim
nroose commented 8 years ago

I guess it's easier if I post part of the error message:

      TypeError: Requested keys of a value that is not an object.
      TypeError: Requested keys of a value that is not an object.
          at http://vm2.repairpal.com:3001/assets/reservations/backbone/templates/discounts_template.js?body=1:79
          at http://vm2.repairpal.com:3001/assets/reservations/backbone/templates/years_select_template.js?body=1:68
          at http://vm2.repairpal.com:3001/assets/reservations/backbone/templates/discounts_template.js?body=1:97
          at http://vm2.repairpal.com:3001/assets/reservations/backbone/views/discounts.js?body=1:18

Seems to me that the templates shouldn't call eachother, no? What's the rational for using global scope for these functions?