A few comments/suggestions

1) instead of

  response.headers['web2py-component-command']="$
('#show_assoc_contact').click(function(event){...);"

you can do

  response.js="jQuery('#show_assoc_contact').click(function(event)
{...);"

Use jQuery instead of '$' since '$" may conflict with non-jquery js
libraries.

2) You can include a js in a component

def oppty_contacts():
     response.files.append(....)
     ...

and you have not problems with markmin pages.

BUT you will no be able to do

    {{=plugin_wiki.widget('oppty_contacts',...)}}

because the response.files.append will be executed after the header is
serialized anyway, if you are using the {{....}} syntax that you are
probably using normal web2py controllers actions and views. In this
case you can do

in function:

     mycomponent=plugin_wiki.widget('oppty_contacts',...)

in view:

     {{=mycomponent}}

Hope this makes sense.

On Aug 13, 6:54 am, Miguel Lopes <mig.e.lo...@gmail.com> wrote:
> In order to include scripting in a very dynamic component I'm appending a js
> file to the response files of every template that uses the component, such
> as:
>
> {{response.files.append(URL(request.application,'static/base_components','view_oppty.js'))}}
> {{extend 'layout.html'}}
>
> However, this has the problem of decoupling the scripting from the
> component. Although, I find the advantage of leaving the js in its own file
> which is convenient for editing and maintenance.
> My reading of the web2py Book chapter 13 suggests that the answer would be
> to minify the js code and include it in the controller, such as:
>
> def oppty_contacts():
>    define var to be used on the template
>    ...
>
> response.headers['web2py-component-command']="$('#show_assoc_contact').click(function(event){...);"
>    form=...
>    return dict(..., form=form)
>
> However I can't get this is to work.
>
> My doubts are:
> I wonder if there is a way to couple a scripting file with the component (so
> I can enjoy the advantage of having the js in its own file)?
> What am I doing wrong in the alternative solution of including the js code
> via response.headers?
>
> Miguel

Reply via email to