3 ms·
> Almost all the sites I'll develop take advantage of progressively enhanced components or at minimum, widgets that are bootstrapped server side. Angular wants
by avolcano 13y ago
> Almost all the sites I'll develop take advantage of progressively enhanced components or at minimum, widgets that are bootstrapped server side. Angular wants you to load everything via AJAX - that can get heavy very quickly.
I'm surprised that I haven't seen a library for any JavaScript framework that elegantly handles loading JSON returned within a server-side template. It seems like the best of both worlds - use your server-side template, but then also get the data as JSON so that you don't have to do a second request to populate your Angular $scope with data.
The easy way to implement this would be a library that automatically added <script> tags containing the JSON that you could then reference from your Angular template. It could even wrap the JSON in an Angular module of some sort to keep your global namespace from being polluted.
- SanderMak 13y agoAgreed. I tried to explore this issue a while back: http://branchandbound.net/blog/web/2012/11/unify-server-side-client-side-rendering-embedding-json/ http://branchandbound.net/blog/web/2012/11/unify-server-side... Turns out that using script-tags is somewhat problematic, but as a comment points out, data-attributes could work.
- esailija 13y agoThis is not a problem in e.g. PHP, out of the box it can embed json in script tags, with default settings you never get the sequence `</script>` (you get `<\/script>` instead) and it even escapes U+2028` and U+2029 which are syntax errors in Javascript string literals but not in JSON.
- conradfr 13y agoOn initial page load I usually put json in a global javascript variable and retrieve it in Angular. Maybe you can do something cleaner with module.value() ?
- goblin89 13y ago> Maybe you can do something cleaner with module.value() ? I do a simple thing like that for certain settings[0]. I imagine this could easily be done for bootstrapped model data as well. [0] https://gist.github.com/strogonoff/6159373 https://gist.github.com/strogonoff/6159373