Skip to main content

Question on publish/subscribe mechanism

11 replies [Last post]
a_laksmana
Offline
Joined: 2007-11-01

I managed to use the default publish subscribe and onSelect event by yahoo dataTable.

For example:

jsp:

glue.js:
// map topics for yahoo dataTable selection
jmaki.subscribe("/yahoo/dataTable/onSelect", function(args) {
jmaki.log("glue.js : onSelect request from: " + args.widgetId);

//implement your method here
});

However I just found out that I cannot specify a different name other than /yahoo/dataTable even though some blogs said it's possible? The objective is to differentiate who is calling the onSelect. Imagine I have table A to Z. I need to be able to call different beans.

I tried doing below steps, but does not work:

at jsp:

at glue.js:
// map topics for yahoo dataTable selection
jmaki.subscribe("/userpage/teamtable/onSelect", function(args) {
jmaki.log("glue.js : onSelect request from: " + args.widgetId);
});

The jmaki logger shows that the teamtable widget is still publishing to /yahoo/dataTable/onSelect.

Please let me know if you know how.
For now, to differentiate who is calling the onSelect. I use the widget id and then if elseif each table. But I think this is not right.

jsp:

glue.js:
// map topics for yahoo dataTable selection
jmaki.subscribe("/yahoo/dataTable/onSelect", function(args) {
jmaki.log("glue.js : onSelect request from: " + args.widgetId);

if("teamtable"==args.widgetId){
//call Bean.method for this teamtable onSelect
}
else if("othertable"==args.widgetId){...}
//so on
});

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
annanna22
Offline
Joined: 2007-11-20

Hi, thank you so much, but it doesnt work.

glue.js

jmaki.subscribe("/autor/onSelect", function(args) {

var autor= args.value.split(',')[0];

if (typeof args.value == "undefined") {

var tempList = new List();

autor = tempList.getAutor();

}

jmaki.doAjax({method: "POST", url:

"searchlist.jsp?autor=" + encodeURIComponent(autor),

callback: function(req) {

var tmp = req.responseText;

var obj = eval("(" + tmp + ")");

jmaki.log("tmp "+ obj);

jmaki.publish("/datatable/clear", {});

jmaki.publish("/datatable/addRows", {value: obj});

} }); } );

widget:

publish="/datatable"

value="{columns : [

{ label : 'Titel', id : 'titel'},

{ label :'Autor', id : 'autor'},

{ label : 'ISBN', id : 'isbn'},

{ label : 'Verlag', id : 'Verlag'} ],

rows : [

]
}" />

the table is empty - but in Firebug and : the values are correct

jMaki Logger:

Publish : Topic: /datatable/addRows message {value : {autor : 'Maria Barbal' , titel : 'Wie ein Stein im Geröll' , isbn : '3887472217' , verlag : 'Transit'}}

Publish Match : : Topic: /datatable/addRows listener {topic : '/datatable/addRows' , target : {functionHandler : function (b) { if (b.message) { b = b.message; } for (var i = 0; i < b.value.length; i++) { _widget.addRow({value:b.value[i]}, false); } }} , action : 'call' , id : 'jmk_13'}

annanna22
Offline
Joined: 2007-11-20

Hi, I found the error in the json array - I forgot a pair of []


the correct code:

[b]out.println("["); [/b]

for (int i=0; i
Bestseller b = list.get(i);

out.print("{ autor : '" + b.getAutor() + "', " +

"titel : '" + b.getTitel() + "', " +

"isbn : '" + b.getIsbn() + "', " +

"verlag : '" + b.getVerlag() + "'}");

if (i < list.size()-1)

out.println(",");

else

out.println();

}

[b] out.println("]");[/b]


thank you

carlavmott
Offline
Joined: 2005-02-10

I think you are not listening to the correct topic. You can set jmaki.debug and jmaki.debugGlue to true in your glue.js and it will show the messages that are published, the payload of each and whether there is a listener for the topic. this is useful in debugging the pub/sub events.

I see on your code that you are publishing the data as follows:
jmaki.publish("/mytable/setValues", obj);

the table however doesn't subscribe to "/mytable" but instead is using the default topic. Add subscribe="/mytable" to the table and it should work. Alternatively you can publish to the default topic which is "/dojo/table" in this case.

let me know if this helps.

carla

annanna22
Offline
Joined: 2007-11-20

thank you too for your answer, but It doesn't work too.
The data are not display in the table.

a_laksmana
Offline
Joined: 2007-11-01

I found out in

/resources/yahoo/dataTable/component.js

It is being hardcoded:
var publish = "/yahoo/dataTable";

To enable using the custom publish topic, just replace the above line with:

var publish;
if(undefined==wargs.publish){
publish = "/yahoo/dataTable";
}
else{publish = wargs.publish;}

Then you can publish to custom topic like:

Message was edited by: a_laksmana

annanna22
Offline
Joined: 2007-11-20

Hi, my problem is the table does not display the values, but the service file for filling the table is correct json and full :
I have a combobox and a table. The table get a value from the combobox and fill all the other columns from a database.


[b]search.jsp:[/b]



name="dojo.combobox"
publish="/autor"
subscribe="/cb"
service="combodata.jsp" />




name="dojo.table"
publish="/cb/getSearchList"
service="searchlist.jsp" />


the data for the widget combobox:

<%
EntityManagerFactory emf = Persistence.createEntityManagerFactory("buchportalPU");

EntityManager em = emf.createEntityManager();

List list = em.createNamedQuery("Bestseller.findAll").getResultList();

out.println("value= [");

for (int i=0; i
Bestseller b = list.get(i);

out.print("{label : '" + b.getAutor() + "'" + "," + " value : '" + b.getAutor() + "'}");

if (i < list.size()-1)

out.println(",");

}
out.println("]");

%>

the data for the table:

<%
String searchParam = request.getParameter("autor");

String autor = searchParam;

if (searchParam == null || "".equals(searchParam))

autor = request.getParameter("autor");

EntityManagerFactory emf = Persistence.createEntityManagerFactory("buchportalPU");

EntityManager em = emf.createEntityManager();

List list = em.createNamedQuery("Bestseller.findByAutor").
setParameter("autor", autor).getResultList();

out.println("{columns : [" +

"{ label :'Autor', id : 'autor'}," +

"{ label :'Titel', id : 'titel'}," +

"{ label :'Isbn', id : 'isbn'}," +

"{ label :'Verlag', id : 'verlag'}" +

"],");

out.println("rows: [");

for (int i=0; i
Bestseller b = list.get(i);

out.print("{ autor: '" + b.getAutor() + "'," +

"titel: '" + b.getTitel() + "'," +

"isbn: '" + b.getIsbn() + "'," +

"verlag: '" + b.getVerlag() + "'}");

if (i < list.size()-1)

out.println(",");

else

out.println();


}
out.println("] }");

%>



and the glue.js for the combobox:

jmaki.subscribe("/autor/onSelect", function(args) {

var autor= args.value.split(',')[0];

if (typeof args.value == "undefined") {

var tempList = new List();

autor = tempList.getAutor();

}

var encodedAutor = encodeURIComponent("autor=" + autor);

var url = jmaki.xhp + "?id=searchlist&urlparams=" + encodedAutor;

jmaki.doAjax({method: "POST", url:
"searchlist.jsp?autor=" + encodeURIComponent(autor),

callback: function(req) {

var tmp = req.responseText;

var obj = eval("(" + tmp + ")");

jmaki.log("tmp "+ obj);

jmaki.publish("/mytable/setValues", obj);

}

});

}
);



please help me, I don't know, why the table not publish the data.

Message was edited by: annanna22

carlavmott
Offline
Joined: 2005-02-10

Hi,

I just looked at the code and publish is overwritten if it is supplied on the tag. You should not have to modify the component.js file as it sets the topic for publishing to "/yahoo/dataTable" and if the publish attribute is specified it is overwritten. Which version of jMaki are you using?

Carla

annanna22
Offline
Joined: 2007-11-20

version : 1.0.3



I set publishing to "/yahoo/dataTable", but it doesn't work better.

I have no idea, why the table is not loading the searchlist.jsp with the complete data.

jmaki.subscribe("/dojo/combobox/onSelect", function(args) {

var autor= args.value;

if (typeof args.value == "undefined") {

var tempList = new List();

autor = tempList.getAutor(); }

var encodedAutor = encodeURIComponent("autor=" + autor);

var url = jmaki.xhp + "?id=searchlist&urlparams=" + encodedAutor;

jmaki.doAjax({method: "POST", url: "searchlist.jsp?autor=" +
encodeURIComponent(autor),

callback: function(req) {

var tmp = req.responseText;

var obj = eval("(" + tmp + ")");

jmaki.log("tmp "+ obj);

jmaki.publish("/yahoo/dataTable", obj);

} }); });


the widgets:






carlavmott
Offline
Joined: 2005-02-10

Sorry I didn't see this last night...

Assuming that the data is correctly formated you need to do the following:

jmaki.publish("/yahoo/dataTable/clear",{});
jmaki.publish("/yahoo/dataTable/addRows", {value: obj});

When you publish the topic name really has 2 parts. Before the last slash is the name of the topic and after the last slash is the command you want to perform or handler you want to invoke. So this means, for example, that for all subscribers to /yahoo/dataTable you want to first invoke the clear handler in the wrapper and then the addRows handler passing it an object with the table data. See my blog on data pagination for more info:

http://weblogs.java.net/blog/carlavmott/archive/2007/09/how_to_implemen....

bakarirum
Offline
Joined: 2005-07-07

Carla,
I understand the two parts of the publish mechanism. However, I am having trouble location what the default handlers are for each widget. I have a Yahoo! Tooltip and I want to change the value of the tooltip using some Javascript code. What is the correct handler to call to do this? Where is a listing for all the default handlers for each widget?


]]>

carlavmott
Offline
Joined: 2005-02-10

We have a set of pages that describe the data and event model. See http://wiki.java.net/bin/view/Projects/jMakiDataModels for more information. I don't see a page for tooltip and I looked at the wrapper code but don't see and handlers in the code. I'll post an email asking for that functionality to the dev list since the developer who wrote that is on that list.

Carla