On Sep 4, 2011, at 6:41 AM, kenji4569 wrote:
> I'v developed several plugins, and I found some of them were
> unavailable when using ajax LOAD, because they requires js and css
> files stored in response.files (e.g. http://dev.s-cubism.com/plugin_uploadify_widget,
> http://dev.s-cubism.com/plugin_anytime_widget).
>
> Specifically, I'd written the follwing code in each plugin file:
>
> def _set_files(files):
> current.response.files[:0] = [f for f in files if f not in
> current.response.files]
>
> It was okey before I used the LOAD helper (ajax=True) with the
> plugin.
>
> So, after struggles, I ended up writing the following code:
>
> def _set_files(files):
> if current.request.ajax:
> current.response.js = (current.response.js or '') + """;
> (function ($) {
> var srcs = $('script').map(function(){return $(this).attr('src');}),
> hrefs = $('link').map(function(){return $(this).attr('href');});
> $.each(%s, function() {
> if ((this.slice(-3) == '.js') && ($.inArray(this.toString(), srcs)
> == -1)) {
> var el = document.createElement('script'); el.type = 'text/
> javascript'; el.src = this;
> document.body.appendChild(el);
> } else if ((this.slice(-4) == '.css') &&
> ($.inArray(this.toString(), hrefs) == -1)) {
> $('<link rel="stylesheet" type="text/css" href="' + this + '" /
>> ').prependTo('head');
> if (/* for IE */ document.createStyleSheet)
> {document.createStyleSheet(this);}
> }});})(jQuery);""" % ('[%s]' % ','.join(["'%s'" % f.lower().split('?')
> [0] for f in files]))
> else:
> current.response.files[:0] = [f for f in files if f not in
> current.response.files]
>
> In this nasty code, js and css files of the plugin are dynamically
> downloaded without re-download.
>
> I am expecting that, it would be possible to use the former short
> "_set_files" function in the plugin for ajax, if web2py recognized the
> response.files in ajax requests.
>
> Further, I'v had to modify a script written in each plugin, for
> example,
>
> From:
> """
> jQuery(function() {
> jQuery("#%(id)s").xxx({...});
> });
> """ % {...}
>
> To:
> """
> jQuery(function() { var t = 10; (function run() {if ((function() {
> var el = jQuery("#%(id)s");
> if (el.xxx == undefined) { return true; }
> el.xxx({...});
> })()) {setTimeout(run, t); t = 2*t;}})();});
> """ % {...}
> (this code waits until required js files are downloaded)
>
> Although it works fine, it needs a bit boilerplate.
> I think it might be good if a function for such a boilerplate is
> provided in global by web2py_ajax.js, or
> response.something.append("""...""") could be used for this.
>
> What do you think? or any alternatives are already provided?
>
> Thanks in advance,
> Kenji
>
> --
> mail from:GoogleGroups "web2py-developers" mailing list
> make speech: web2py-d...@googlegroups.com
> unsubscribe: web2py-develop...@googlegroups.com
> details : http://groups.google.com/group/web2py-developers
> the project: http://code.google.com/p/web2py/
> official : http://www.web2py.com/
What it is useful is having a standard way to push changes into the
loading page using the controller.
For instance
response.ajax_header_contributions=dict(css=[...], js=[...])
response.ajax_before=<js code>
repsonse.ajax_after=<js code>
and so on. The above info can be sent back as json to the caller ajax
function that decodes it and takes appropriate actions.
Compatibility can be kept by using another ':<target>' target type in
ajax function.
mic
2011/9/7 Massimo Di Pierro <massimo....@gmail.com>: