How can I handle JSON response in Moqui inside a transition? - moqui

I am actually making a JSON-RPC call to a transition from AngularJS. As the content type of the request is application/json, the input parameters are automatically made available in the context so I don't need to handle it explicitly.
Here is my request to 'getUsers' which is in the 'tutorial' component.
function($scope, $http) {
$http ({
url: 'tutorial/getUsers',
method: "POST",
data: JSON.stringify({username:'demouser1'}),
headers: {'Content-Type': 'application/json'}
}).success(function(response){
$scope.userList = response;
});
}]);
The code for the transition is written below
<transition name="getUsers">
<actions>
<service-call name="Tutorial.PartyServices.get#Users" in-map="context"/>
</actions>
<default-response type="none"/>
</transition>
The service looks like this
<service verb="get" noun="Users">
<in-parameters>
<parameter name="username"/>
</in-parameters>
<actions>
<entity-find entity-name="PersonAndUserAccount" list="userList">
<search-form-inputs default-order-by="firstName,lastName,username"/>
<econdition field-name="username" operator="equals" to-field-name="username"/>
</entity-find>
<script>ec.web.sendJsonResponse(userList)</script>
</actions>
</service>
Now to send JSON response I wrote ec.web.sendJsonResponse(userList) in the service itself. This makes the service tightly coupled with service calls that expect JSON as response. If I want to internally call this service i.e. in Moqui I would have to define another service.
So my question is can I handle this response in the transition which is making this service call?

Here is the sample available for mentioning JSON response in transitions. You can refer ExampleApp.xml.
Here is the code snippet for your reference.
<transition name="ExampleEntity" method="post">
<service-call name="org.moqui.example.ExampleServices.createExample" in-map="ec.web.parameters"
web-send-json-response="true"/>
<default-response type="none"/>
</transition>
HTH.
Thanks

Related

nuxtJS how to make javascript load dynamically by page

I would like to insert this script to nuxtJS project and I want it to load dynamically by page.
<!-- LINE Tag Base Code -->
<!-- Do Not Modify -->
<script>
(function(g,d,o){
g._ltq=g._ltq||[];g._lt=g._lt||function(){g._ltq.push(arguments)};
var h=location.protocol==='https:'?'https://d.line-scdn.net':'http://d.line-cdn.net';
var s=d.createElement('script');s.async=1;
s.src=o||h+'/n/line_tag/public/release/v1/lt.js';
var t=d.getElementsByTagName('script')[0];t.parentNode.insertBefore(s,t);
})(window, document);
_lt('init', {
customerType: 'account',
tagId: 'xxxxx'
});
_lt('send', 'pv', ['xxxxx']);
</script>
<noscript>
<img height="1" width="1" style="display:none"
src="https://tr.line.me/tag.gif?c_t=lap&t_id=xxxxx&e=pv&noscript=1" />
</noscript>
<!-- End LINE Tag Base Code -->
The important thing is tagId: 'xxxxx' this should be change dynamically by page. for example,
wwww.sample.com/shop1 will load tagId: 'shop_tag1'
wwww.sample.com/shop2 will load tagId: 'shop_tag2'
What kind of tracker is this ? If it does not have any way of setting it app-wise, I guess that you need to have a middleware that is running your function and grabbing the dynamic part from each page.
This can be helpful: https://nuxtjs.org/docs/2.x/directory-structure/middleware/
Also, I did found some issues on using a middleware in a layout, but maybe it's just me, if it does work on your side, you could just dump your js there too.

React server-side and client side rendering not seamless

First the page was rendered by the server, then on client/ browser side, the Javascript script re-render the whole page!
I don't think this is how it's supposed to be since it's very bad user experience.
One thing I noticed is that the data-reactid attribute of my root element as rendered by server is some hash like .2t5ll4229s and all the children has that as prefix e.g. .2t5ll4229s.0 (the first child). Whereas, on the browser side, the data-reactid is .0 for the root element and .0.0 for the first child.
If data-reactid is really the culprit here, is there a way to set it a value of choice like eric123 for both client side and server side.
If data-reactid is not the culprit here, how do I go about making server and client side rendering of React seamless i.e. only certain elements should be re-rendered by the client side, not everything!?
My index-server-local.html template:
...
<body>
<div id="content" class="container-fluid wrapper no-padding-left no-padding-right">
{{{reactHtml}}}
</div>
<script src="bundle.js"></script>
</body>
...
My server.js:
server.get('*', function (req, res) {
console.log('request url', req.url);
log.debug('routes are', JSON.stringify(routes));
res.header("Access-Control-Allow-Origin", "*");
match({routes, location: req.url}, (error, redirectLocation, renderProps) => {
if (renderProps) {
let htmlStr = React.renderToString(<RoutingContext {...renderProps} />);
res.render('index-server-local', { reactHtml: htmlStr });
}
}
My browser.js:
React.render(<Router history={history} routes={routeConfig} />, document.getElementById('content'));
I'm using react-router 1.0.0 and React 0.13.3.
We need serialize data(or state) in server side, and send it to client side to deserialize, otherwise, the data in client side is different with the moment server render it. it will reload for sure.
One exception: pure static page, in this case React recommend us use renderToStaticMarkup
Similar to renderToString, except this doesn't create extra DOM attributes such as data-react-id, that React uses internally. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save lots of bytes.
So, how we serialize - deserialize?
Here is a simple version:
in your index-server-local.html template:
<script src="bundle.js"></script>
to:
<script dangerouslySetInnerHTML={{{{__html: 'window.__data=' + JSON.stringify({key: 'value'})}}}} />
<script src="bundle.js"></script>
And in client side, we can use __datadata now. how to map the data to your component it's based on your choice.
I recommend Reudx for this:
createStore(browserHistory, initialState)
And then
<Provider store={store}>
{ component }
</Provider>

API REST SoapUI POST - BOSE Speaker API

OK apologies, I have never used REST and i'm struggling to get to grips with an API.
So i'm using soapui to process the requests. I can run GET fine as it's just returning data, but I cannot work out how to post.
for example:
http://10.0.1.75:8090/info
Returns all info about the device.
So to control the device I need to send this:
http://10.0.1.75:8090/key
<key state="press" sender="Gabbo">$KEY_VALUE</key>
<key state="release" sender="Gabbo">$KEY_VALUE</key>
This seems so simple I must be missing something but I cannot see how to post these parameters in soapui? Once i've worked out one I can write the whole API.
Thanks in advance.
This example should answer your question:
<body>
<button onclick="postVolume(0)">Volume 00</button>
<button onclick="postVolume(10)">Volume 10</button>
<button onclick="postVolume(20)">Volume 20</button>
<button onclick="postVolume(30)">Volume 30</button>
<button onclick="postVolume(40)">Volume 40</button>
<script>
function postVolume(volume) {
var xml = '' +
"<?xml version='1.0' encoding='UTF-8' ?>" +
"<volume>" + volume + "</volume>";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","http://10.0.1.75:8090/volume",true);
xmlhttp.send(xml);
};
</script>
</body>
Hope this will help you :-)

How can I react to a finished upload with <h:inputFile>?

I'm using Richfaces-4.3.5 on Wildfly-8.0.0.CR1 here, migrating from <rich:fileUpload> which doesn't work with JSF-2.2/Servlet-3.0. I'm replacing it with this snippet:
<rich:popupPanel id="testPop" autosized="true">
<h:form id="uploadF" enctype="multipart/form-data">
<h:inputFile value="#{bean.file}">
<a4j:ajax listener="#{bean.storeFile()}" render="#form,:fileListTbl"
oncomplete="#{rich:component('testPop')}.hide();" />
</h:inputFile>
</h:form>
</rich:popupPanel>
This works fine in that the storeFile method is called and I can access bean.file just fine. However, I'd like to close the rich:popupPanel when I'm done uploading, so I need to react to the success/complete events of the ajax request. But that doesn't seem possible - the popup stays visible and the response is clearly incomplete (indented for better readability):
<?xml version='1.0' encoding='UTF-8'?>
<partial-response id="j_id1">
<changes>
<update id="j_id1:javax.faces.ViewState:0">
<[CDATA[-1186354868983349335:-5499969782208038164]]>
</update>
<extension id="org.richfaces.extension"><render>#component</render></extension>
</changes>
</partial-response>
Though the richfaces debug messages indicate the handlers are being called:
RichFaces: Received 'success' event from <input id=uploadF:j_idt1136 ...>
RichFaces: Received 'complete' event from <input id=uploadF:j_idt1136 ...>
So, simple question: how can I get the popup to close and the components to be re-rendered?
i'm not sure if the problem is directly related to a4j:ajax or what's needed to be done to make it work with a4j:ajax, but the code below seems to work with f:ajax.
<h:form id="formId">
<h:commandLink value="popup"
onclick="#{rich:component('testPop')}.show(); return false;" />
</h:form>
<rich:popupPanel id="testPop" autosized="true">
<h:form id="uploadF" enctype="multipart/form-data">
<h:inputFile value="#{bean.file}">
<f:ajax listener="#{bean.storeFile()}" render="#form :fileListTbl"
onevent="eventHandler"/>
</h:inputFile>
</h:form>
</rich:popupPanel>
<script>
function eventHandler(event)
{
if (event.status == 'success')
{
#{rich:component('testPop')}.hide();
}
}
</script>

Ajax call not working in Google gadget

I have a conversion rate script which i know works perfectly outside of a Google gadget however i cannot figure out why it doesn't work inside of a gadget.
Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs author="Purefx.co.uk" height="280"></ModulePrefs>
<UserPref name="title" display_name="Widget Title" default_value="Currency Converter"/>
<UserPref name="color" display_name="Widget color" default_value="Color" datatype="enum">
<EnumValue value="Color"/>
<EnumValue value="Black and White"/>
</UserPref>
<UserPref name="style" display_name="Widget Style" default_value="Sidebar" datatype="enum">
<EnumValue value="Sidebar"/>
<EnumValue value="header/footer"/>
</UserPref>
<UserPref name="attribution" display_name="Attribution text" default_value="Purefx" datatype="enum">
<EnumValue value="Purefx"/>
<EnumValue value="Foreign Exchange"/>
<EnumValue value="Currency exchange"/>
</UserPref>
<Content type="html"><![CDATA[
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#convert').click(function(){
//Get all the values
var amount = $('#amount').val();
var from = $('#from').val();
var to = $('#to').val();
//Make data string
var dataString = "amount=" + amount + "&from=" + from + "&to=" + to;
$.ajax({
type: "POST",
url: "ajax_converter.php",
data: dataString,
success: function(data){
//Show results div
$('#results').show();
//Put received response into result div
$('#results').html(data);
}
});
});
});
</script>
]]>
</Content>
</Module>
I haven't included the html part of the content or the php script as that part is 100% working and irrelevant to this problem.
I think the problem is specifically the execution of the Ajax call, on clicking 'convert' nothing is being 'posted' in the firebug console window.
I can't find anything that might suggest i'm missing something so any thoughts are appreciated.
Many thanks in advance
You cannot make direct calls from inside a gadget because a gadget lives inside a gadget container and all calls are proxied by the gadget container.
You must use io.makeRequest to fetch remote data.
More info, see http://code.google.com/apis/gadgets/docs/remote-content.html