views:

243

answers:

1

I am creating a mixin which renders a javascript file when a textfield gains focus.

I am new to the idea of mixins in Tapestry, and I am unsure of where to place my original javascript file which i wish to run when the textfield gains focus.

The following is an example of my code: The Java mixin class:

package asc.mixins;

import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.corelib.base.AbstractTextField;

@IncludeJavaScriptLibrary("js_dasher_mixin.js")
public class DasherMixin {

@Environmental
private RenderSupport renderSupport;

@InjectContainer
private AbstractTextField field;

@AfterRender
void addScript() {
    this.renderSupport.addScript("new JSDasher('%s');", 
            this.field.getClientId());
 }


}

The Javascript mixin file:

JSDasher = Class.create({

initialize: function(textField) 
{
    this.textField = $(textField);

    this.textField.observe('focus', this.onFocus.bindAsEventListener(this));
},

onFocus: function(event)
{
    //call my javascript init() function
} 
}

part of my javascript file I wish to run when the textfield gains focus:

var posX, posY;


// Sets up our global variables and dispatches an init request to the server.
function init() {

posX=0;
posY=0;
canvas = document.getElementById("canvas"); 
canvasWidth = canvas.offsetWidth;
canvasHeight = canvas.offsetHeight;
if (canvas.getContext) {   
    ctx = canvas.getContext("2d");
}

canvas.onclick = canvasClicked;
canvas.onmousemove = mouseMoved; 

canvasOffsetX = findPosX(canvas);
canvasOffsetY = findPosY(canvas);

sessID = -1;

sendInitRQ(canvasWidth, canvasHeight);

}

My javascript file is much larger than above, my question is where should I put my javascript code above?

Should it all be contained in the mixin.js file? if so where exactly should it go?

Thanks in advance for any help.

+1  A: 

The method is ok free-floating in mixin.js but with name like init, you might have a conflict. You can put it in the JSDasher class itself or move the body to the onFocus function. You can also define any other functions on the JSDasher class and call them with this.function_name. Look at datefield.js in the Tapestry source as an example.

Brian Deterling
Thanks for your response. I am still a bit unclear maybe I didn't phrase my question correctly. I know where to put the mixin.js file, I am just unsure whether i should have my original js code seperate from the mixin.js file. If so how can i call the init() method from my original js code. Should it be included somehow in the mixin.js?I hope this makes sense. Thanks again.
shane87
I updated the answer because I misunderstood the question.
Brian Deterling
@Brian Deterling: Thanks again..just one more question.Is it possible to call a method from another javascript file from the mixin.js file. My javascript file is very big and sends requests to a php socket running on another server. So i need to keep my javascript code in the root of the other server along with the php socket. i.e. I need to keep the mixin.js code seperate from my other javascript code.What I would like to do is somehow hook into the external javascript code in the onfocus event. Any help here would be great.
shane87
If the JavaScript code is loaded into the user's browser on the same page, then you can call it. You can load JavaScript files from multiple servers so mixin.js can come from your Tapestry server and another .js file can come from the PHP server. As long as the external one has loaded in the browser before mixin.js tries to call functions in it, you should be ok. So if I understand you correctly, you should have add a line like <script src="http://the_php_server/big.js"/> in your page or layout. You might also be able to do that with @IncludeJavaScriptLibrary using the full URL.
Brian Deterling