Skip to main content

how do I force a call to a function during load

4 replies [Last post]
danop
Offline
Joined: 2006-07-10
Points: 0

Hello all,

I'm new to jmaki and am very excited to explore this concept, but I have an issue and I cannot seem to figure out how to force a call to a function in component.js script during the page load. Basically, I have a jmaki component that is modeled after the jmaki list component, and am trying to have a method execute when my page loads. Any help is greatly appreciated... tia

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
danop
Offline
Joined: 2006-07-10
Points: 0

I apologize if my question was not clear... Is there anything I can explain further in detail to receive some assistance? Thanks...

danop
Offline
Joined: 2006-07-10
Points: 0

I forgot to add...
I was looking that jmaki.js script and from what i can understand, it looks like it is invoking the onLoad method if the jmaki component's onLoad attribute is not null. Am i missing an event declaration or do i have a syntax issue? Thanks.

[b]Here is the reference in my main jsp page:
[/b][i]

[b]Here is a portion of component.js:[/b]
[i]function List() {
var uuid = widget.uuid;
var service = widget.service;

function getXHR(url) {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}

this.loadScript = function() {
var listcontrol = document.getElementById(uuid + "_productSelection");
var req = getXHR(service);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
listcontrol.innerHTML = req.responseText;
}
}
};
req.open("POST", service, true);
var selectField = document.getElementById(uuid + "_productSelection");
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send("command=load&entry=" + selectField.value + "&uuid=" + uuid);
}
[/i]

[b]Here is a portion of component jsp:
[/b][i]

    <%
    String command = request.getParameter("command");
    String uuid = request.getParameter("uuid");
    String service = request.getParameter("service");
    String item = "";
    com.testjmaki.beans.ProductRetriever pr = new com.testjmaki.beans.ProductRetriever();

    if ("load".equals(command)){
    pr.loadProducts();
    while(pr.next()){
    out.println("");
    }
    }
    [/i]

gmurray71
Offline
Joined: 2003-07-31
Points: 0

The component.js is evaluated when your page is loaded which will result in the the entire script being loaded.

You can create the List object and then call the locadScript function on the object:

For example:

function List() {
var uuid = widget.uuid;
var service = widget.service;

this.loadScript = function() {

var listcontrol = document.getElementById(uuid + "_productSelection");
jmaki.doAjax({url: url, callback: function(req) {
if (req.readyState == 4) {
if (req.status == 200) {
listcontrol.innerHTML = req.responseText;
}}
}});

var selectField = document.getElementById(uuid + "_productSelection");
var bodyText = "command=load&entry=" + selectField.value + "&uuid=" + uuid;
jmaki.doAjax({url: service, method: 'post', body: bodyText});
}

var list = new List();
list.loadScript();
// put the list in the jmaki attributes map if you plan to access it later
jmaki.attributes.put(widget.uuid, list);

Notice that I am using the jmaki XmlHttpRequest for doing both the post and get. The reason for this is that in evaluated code you can not make an XmlHttpRequest as it would be a security violation. Your other option would be to create your own library and make it available as a type (e.g. put a reference int the types.properties file).

Please let me know if this script gives you any problems.

danop
Offline
Joined: 2006-07-10
Points: 0

Awesome! That works.. You Rock! JMaki Rocks! Thanks a bunch!