You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Javix <s....@gmail.com> on 2011/12/28 17:40:12 UTC

Difference between renderSupport.addScript and JSON addInit(function, JSON Object)

I have a strange behavior when usin the above methods to add javascript from
inside Java classes.
When I use the first one like this:

//java class

@Inject
    private Messages messages;

    @Environmental
    private RenderSupport renderSupport;
 @BeginRender
    void addJsLibs() {
	String js = String.format("load(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\",
\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\");",
	    messages.get("new_delivery_address"),
	    messages.get("js.error.address.empty"),
	    messages.get("js.error.receiving.person.empty"),
	    messages.get("js.error.street.empty"),
	    messages.get("js.error.house.empty"),
	    messages.get("js.error.postcode.empty"),	 
	    messages.get("js.error.postcode.wrong"),
	    messages.get("js.error.city.empty"),
	    messages.get("js.error.special.chars.not_accepted"),
	    messages.get("js.error.floor.empty"),
	    messages.get("js.error.floor.wrong"),
	    messages.get("js.error.elevator.empty"));
	    
	    renderSupport.addScript(js);	
    }

In javascript file:
function load(m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12){
...
//access to the passed variables, no problems.
}

It works.

When I replace in Java class the above code with JSON object:

JSONObject jsonObject = new JSONObject();
	    jsonObject.put("m1", messages.get("js.error.password.epmpty"));
	    jsonObject.put("m2",
messages.get("js.error.confirmed_password.empty"));
	    jsonObject.put("m3", messages.get("js.error.password.not_matched"));
	    jsonObject.put("m4", messages.get("js.error.email.empty"));
	    jsonObject.put("m5", messages.get("js.error.email.wrong"));

            renderSupport.addInit("loadPass", jsonObject);
}
In javascript, I just replaced arguments by jsonObject:

function load(jsonObject){
...
//acces to the variable in JSON
errorText.text(jsonObject.m2)
}

Why in case of JSON it DOES NOT WORK ? What it depends on. More of that, I
have pages and classes wher JSON works as described before, but a lot of
cases where it does not.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Difference-between-renderSupport-addScript-and-JSON-addInit-function-JSON-Object-tp5105620p5105620.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Difference between renderSupport.addScript and JSON addInit(function, JSON Object)

Posted by Javix <s....@gmail.com>.
Finally I got it working by adding as you told:

Tapestry.Initializer.load = function load(jsonObject){
..
errorText.text(jsonObject.m1)

}

and ot in the very begining of the call:

jQuery(document).bind(Tapestry.ZONE_UPDATED_EVENT, function(e){	
	//load(m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12);
	load(jsonObject);
});
It works, THANK YOU!

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Difference-between-renderSupport-addScript-and-JSON-addInit-function-JSON-Object-tp5105620p5119608.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Difference between renderSupport.addScript and JSON addInit(function, JSON Object)

Posted by Javix <s....@gmail.com>.
It didn't work:

Oups ... org.apache.tapestry5.runtime.ComponentEventException: A component
event handler method returned the value
MultiZoneUpdate[{page=org.apache.tapestry5.corelib.components.Zone@1799a64, 

May it's because in the javascript file the call to the load fucntion is
made lie that:

jQuery(document).bind(Tapestry.ZONE_UPDATED_EVENT, function(e){	
	//load(m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12);
	load(jsonObject);
});

...
Tapestry.Initializer.load = function load(jsonObject){
//function load(m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12){
errorText.text(jsonObject.m1)
...
}

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Difference-between-renderSupport-addScript-and-JSON-addInit-function-JSON-Object-tp5105620p5119428.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Difference between renderSupport.addScript and JSON addInit(function, JSON Object)

Posted by Javix <s....@gmail.com>.
One more question:
How to trigger the exectution of js function. For exmaple, before the code
was like that directly n the tml page:

<t:if test="error">
							<div class="info i-error">
								Some hard-coded meessage
							</div>
							
</t:if>
So I tried to apply your technics, but the js function is never loaded (I
have no errors displayed when submitting the form):
@IncludeJavaScriptLibrary(value = { "context:/static/javascript/password.js"
})
public class ForgottenPassword {
...
@Property
    private boolean error;

@BeginRender
    void addJsLibs() {
	if (error) {
	    JSONObject jsonObject = new JSONObject();
	    jsonObject.put("m1", messages.get("js.error.password.epmpty"));
	    jsonObject.put("m2",
messages.get("js.error.confirmed_password.empty"));
	    jsonObject.put("m3", messages.get("js.error.password.not_matched"));
	    jsonObject.put("m4", messages.get("js.error.email.empty"));
	    jsonObject.put("m5", messages.get("js.error.email.wrong"));    	    
	    
	    renderSupport.addInit("loadPass", jsonObject);
	}
    }
...
}
And in JS file password.js:

Tapestry.Initializer.load = function loadPass(jsonObject){
..
passError.html(jsonObject.m1);
...
}
loadPass(jsonObject);

Why?






--
View this message in context: http://tapestry.1045711.n5.nabble.com/Difference-between-renderSupport-addScript-and-JSON-addInit-function-JSON-Object-tp5105620p5125897.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Difference between renderSupport.addScript and JSON addInit(function, JSON Object)

Posted by Javix <s....@gmail.com>.
OK, I come back to the very beginning.
Before, I had a page with all the text, js and errors hard coded:

<t:parameter name="else">
					<t:if test="forgotten">
						<t:if test="error">
							<div class="info i-error">
								Podany adres nie jest zarejestrowany w naszym serwisie.
							</div>
							
						</t:if>
...
<t:parameter name="additionalJsBottom">	
		
	</t:parameter>

So I replaced it as follows:

In java class:

@IncludeJavaScriptLibrary({"context:static/javascript/password.js"})
public class ForgottenPassword {

@Inject
    private Messages messages;
    
    @Environmental
    private RenderSupport renderSupport;

    @BeginRender
    void addJsLibs() {
	if (error) {
	    JSONObject jsonObject = new JSONObject();
	    jsonObject.put("m1", messages.get("js.error.password.epmpty"));
	    jsonObject.put("m2",
messages.get("js.error.confirmed_password.empty"));
	    jsonObject.put("m3", messages.get("js.error.password.not_matched"));
	    jsonObject.put("m4", messages.get("js.error.email.empty"));
	    jsonObject.put("m5", messages.get("js.error.email.wrong"));  	    	    	    
	    renderSupport.addInit("loadPass", jsonObject);	    
	    
	}
    }
..
}

I deleted completely the js function call from the tml page.
I modifiied the corresponding js file (password.js):

Tapestry.Initializer.load = function loadPass(jsonObject){	
	console.log("-------------- ENTERED in loadPass");
	jQuery('#changePassLink').click(function(event){
       ...
       passError.html(jsonObject.m1);
.....
	});
}

loadPass(jsonObject);

When I click on submit button of the form, no error message is displayed,
there is notheing in the console of Firebug. So I don't really know where to
check. Every tie I remove cookies and browser cache to be sure that js
modification shall be applied.

The first time I load the page, Firebug displays: "loadPass is not defined. 	
loadPass(jsonObject);".



Here is what I have in the generated source code of the page:

<script type="text/javascript">Tapestry.onDOMLoaded(function() {
Tapestry.init({"linkZone":[["forgottenForm","passZone","/konto/zapomnialem_hasla.forgottenform"]],"zone":["passZone"]});
});

So, the needed fucntion is not loaded. May be it is because of the check in
Java class:

 @BeginRender
    void addJsLibs() {
	if (error) { //Do I really need to check it here ????
	    JSONObject jsonObject = new JSONObject();
	    jsonObject.put("m1", messages.get("js.error.password.epmpty"));
	    jsonObject.put("m2",
messages.get("js.error.confirmed_password.empty"));
	    jsonObject.put("m3", messages.get("js.error.password.not_matched"));
	    jsonObject.put("m4", messages.get("js.error.email.empty"));
	    jsonObject.put("m5", messages.get("js.error.email.wrong"));  	    	    	    
	    renderSupport.addInit("loadPass", jsonObject);	    
	    
	}
    }

Do I really need to check the value of 'errors' variable to call the js
function?

Thanks you in advance.










--
View this message in context: http://tapestry.1045711.n5.nabble.com/Difference-between-renderSupport-addScript-and-JSON-addInit-function-JSON-Object-tp5105620p5139535.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Difference between renderSupport.addScript and JSON addInit(function, JSON Object)

Posted by Steve Eynon <st...@alienfactory.co.uk>.
in address.js remove the last call to loadAddresse. i.e. just have

Tapestry.Initializer.loadAddresse = function loadAddresse(jsonObject){
...
}

it is probably that last surplus : loadAddresse(jsonObject); that
caused the JS error ('cos there is no global function called
loadAddresse) which then disabled JavaScript for the rest of the page
load.

Steve.




On 20 June 2012 15:56, baptiste <ba...@atos.net> wrote:
> It doesn't work for me.
>
> In java :
>   @IncludeJavaScriptLibrary({ "context:static/javascript/address.js" })
>
>   @Environmental
>    private RenderSupport renderSupport;
>
>    @BeginRender
>    void addJsLibs() {
>        log.debug("BEGIN addJsLibs");
>        JSONObject jsonObject = new JSONObject();
>        jsonObject.put("m1", messages.get("new_delivery_address"));
>        jsonObject.put("m2", messages.get("js.error.address.empty"));
>        jsonObject.put("m3", messages.get("js.error.receiving.person.empty"));
>        jsonObject.put("m4", messages.get("js.error.street.empty"));
>        jsonObject.put("m5", messages.get("js.error.house.empty"));
>        jsonObject.put("m6", messages.get("js.error.postcode.empty"));
>        jsonObject.put("m7", messages.get("js.error.postcode.wrong"));
>        jsonObject.put("m8", messages.get("js.error.city.empty"));
>        jsonObject.put("m9", messages.get("js.error.special.chars.not_accepted"));
>        jsonObject.put("m10", messages.get("js.error.floor.empty"));
>        jsonObject.put("m11", messages.get("js.error.floor.wrong"));
>        jsonObject.put("m12", messages.get("js.error.elevator.empty"));
>        renderSupport.addInit("loadAddresse", jsonObject);
>        log.debug("END addJsLibs");
>    }
>
> in address.js:
> Tapestry.Initializer.loadAddresse = function loadAddresse(jsonObject){
> ...
> }
> loadAddresse(jsonObject);
>
> In browser console:
> loadAddresse is not defined
> and method in loadAddresse can never be called
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Difference-between-renderSupport-addScript-and-JSON-addInit-function-JSON-Object-tp5105620p5713987.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Difference between renderSupport.addScript and JSON addInit(function, JSON Object)

Posted by baptiste <ba...@atos.net>.
It doesn't work for me.

In java : 
   @IncludeJavaScriptLibrary({ "context:static/javascript/address.js" })
  
   @Environmental
    private RenderSupport renderSupport;

    @BeginRender
    void addJsLibs() {
    	log.debug("BEGIN addJsLibs");
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("m1", messages.get("new_delivery_address"));
	jsonObject.put("m2", messages.get("js.error.address.empty"));
	jsonObject.put("m3", messages.get("js.error.receiving.person.empty"));
	jsonObject.put("m4", messages.get("js.error.street.empty"));
	jsonObject.put("m5", messages.get("js.error.house.empty"));
	jsonObject.put("m6", messages.get("js.error.postcode.empty"));
	jsonObject.put("m7", messages.get("js.error.postcode.wrong"));
	jsonObject.put("m8", messages.get("js.error.city.empty"));
	jsonObject.put("m9", messages.get("js.error.special.chars.not_accepted"));
	jsonObject.put("m10", messages.get("js.error.floor.empty"));
	jsonObject.put("m11", messages.get("js.error.floor.wrong"));
	jsonObject.put("m12", messages.get("js.error.elevator.empty"));
	renderSupport.addInit("loadAddresse", jsonObject);
	log.debug("END addJsLibs");
    }

in address.js:
Tapestry.Initializer.loadAddresse = function loadAddresse(jsonObject){
...
}
loadAddresse(jsonObject);

In browser console:
loadAddresse is not defined
and method in loadAddresse can never be called

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Difference-between-renderSupport-addScript-and-JSON-addInit-function-JSON-Object-tp5105620p5713987.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Difference between renderSupport.addScript and JSON addInit(function, JSON Object)

Posted by Javix <s....@gmail.com>.
I found why it didn't work. I just replaced:

Tapestry.Initializer.load = function loadPass(jsonObject){	
....
}

with 

Tapestry.Initializer.loadPass = function loadPass(jsonObject){	
...
}

I believed that 'load' was a maethod to call on Tapestry.Initializer
object(unfortunately T API says nothing about that).
Of course I removed the 'if' check for 'error' variable in the Java class,
if not, the function is never loaded.
Cheers. And thank you for your time :)

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Difference-between-renderSupport-addScript-and-JSON-addInit-function-JSON-Object-tp5105620p5139660.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org