hearsayit / HearsayRequireJSBundle

RequireJS integration for Symfony2.
130 stars 55 forks source link

Add start option and data-start script attribute to support additional approach to multi-page sites #40

Closed benglass closed 11 years ago

benglass commented 11 years ago

So I can avoid wrapping all of my main modules in a require call to load a common js file I prefer to use this approach to have the main script look for a data-start attribute and require that module if available. In order to support this I had to add an extra option to the initialize method for the start script.

Alternately if the options were just passed through directly from the twig template to the initialize call then I could just override the initialization template, but since it only passed through main and config options I had to modify the helper.

This stack overflow post discusses this approach to multi page requirejs: http://stackoverflow.com/questions/11674824/how-to-use-requirejs-build-profile-r-js-in-a-multi-page-project/11730147#11730147

Great bundle!

ihortymoshenko commented 11 years ago

@afishnamedsquish, thanks for the contribution. I've looked the link and I think it is quite interesting topic. I've found this repository and looks like this approach can be applied for us too without any changes in the code. Did you try it?

benglass commented 11 years ago

I am familiar with the example you linked. I prefer not to have to declaring a dependency on the main/common module in my submodules because its not really a dependency. Its more that the main module is a global module run on all pages and the submodule is page specific. The main module is also often used as a way to build commonly used assets into a single file but I would still declare those dependencies in submodules if I was going to use them there.

In any case I am moving away from the using the approach I linked as the need to be able to pass configuration options generated at run time to the initialization of the per-page module (eg ContactForm.init({ foo: 'bar' }) where the value of bar is determined by the view) requires a more manual bootstrapping of dependencies.

You can consider this closed although I do think it would be a nice enhancement to just pass through whatever variables were passed to the require_js_initialize() twig function to the initialize.html.twig template as variables as it would make it much more flexible.

ihortymoshenko commented 11 years ago

@afishnamedsquish, i think it is possible to pass any options into the initialize template. However, I don't like such approach and I think there is not anything terrible in declaring dependency.

benglass commented 11 years ago

@IgorTimoshenko while you can pass any options you want into the require_js_initialize twig function, they are not actually passed to the initialize.html.twig template when it is rendered in the RequireJSHelper::initialize method, only the main key. If you just pass the options straight through to the template it would make the templating more flexible as you could pass in any extra variables you want available for use in a custom initialize.html.twig template

ihortymoshenko commented 11 years ago

@afishnamedsquish, I meant that it is possible to do it :)

benglass commented 11 years ago

I understand the objection to supporting the data-start attribute although it seems that providing a way to specify a custom template but not a way to pass any custom variables to it really limits the useful of a custom template.

ihortymoshenko commented 11 years ago

@afishnamedsquish, I agree, it makes sense, I'll add the ability to pass extra options into the initialize template.