quirkey / sammy

Sammy is a tiny javascript framework built on top of jQuery, It's RESTful Evented Javascript.
http://sammyjs.org
MIT License
2.99k stars 383 forks source link

Sammy cannot render templates hosted on a different domain than the origin #104

Closed giacecco closed 13 years ago

giacecco commented 13 years ago

I don't know if it is correct to classify this as an issue or feature request. I wonder if there is a solution already, though.

I have realised today that Sammy cannot import templates (moustache templates in my case) from a different domain name in the same way an HTML page cannot import JavaScript files from another domain name. The error message I get is exactly the same: XMLHttpRequest cannot load [template url.html]. Origin [origin domain] is not allowed by Access-Control-Allow-Origin.

As the error message states clearly, it is all about XMLHttpRequest. I wonder if it can be solved in the same way as for JavaScript files: soes a solution similar to JSONP exist for loading templates? Thanks,

Giacecco

quirkey commented 13 years ago

Interesting idea - The source of the issue, as you assumed is that load internally calls jQuery.ajax, theres probably a way to load cross domain by doing a JSON-P like load and putting the content in <script> tags, but I'm unsure if this belongs in Sammy itself. FWIW, My current recommendation for loading templates is to embed them in the initial page load in <script> tags to avoid making additional AJAX requests.

nickdunn commented 12 years ago

I hit this today, using Sammy for a widget that can be embedded on any URL but JS served from one source. Cross domain can be handled by jQuery's JSONP support which could potentially be supported by Sammy's load method. The caveat is that the templates would need to be served via a server side script in order to wrap the response in the callback function, and to output the template as JSON too.

I'm likely going to implement this as a plugin for my project, but it feels as though this should be officially supported in some way as I see it being a frequent use case (for my projects at least!).