Pointer_stringify is returning garbled text - javascript

I have have the following lib.jslib file
mergeInto(LibraryManager.library, {
IsGuestUser: function (objectName, objectMethodName) {
gamesmart.user.isGuest(function (result) {
console.log(Pointer_stringify(objectName), Pointer_stringify(objectMethodName), result);
gameSmartGameInstance.SendMessage(Pointer_stringify(objectName), Pointer_stringify(objectMethodName), result);
});
}
});
Which gets called from here:
namespace GameSmart {
public class User : API {
[DllImport("__Internal")]
public static extern void IsGuestUser(string objectName, string objectMethodName);
public static void IsGuest(string objectName, string objectMethodName) {
IsGuestUser(objectName, objectMethodName);
}
}
}
And is initiated like so:
public class Test : MonoBehaviour {
void Start() {
GameSmart.User.IsGuest("GameSmart", "OnIsGuest");
}
}
As seen above I pass GameSmart and OnIsGuest to the JavaScript, and when it gets to the JavaScript I call Pointer_stringify() on both of the values.
When converted and logged, I get the following output: 0Zހ𐀀 and ﳀ� I should have gotten GameSmart and OnIsGuest back but I didn't what is causing this to happen?

So the fix for this was to move Pointer_stringify outside of the anonymous function so It looks like this:
mergeInto(LibraryManager.library, {
IsGuestUser: function (objectName, objectMethodName) {
var jsObjectName = Pointer_stringify(objectName);
var jsObjectMethodName = Pointer_stringify(objectMethodName);
gamesmart.user.isGuest(function (result) {
gameSmartGameInstance.SendMessage(jsObjectName, jsObjectMethodName, result);
});
}
});

Related

pusher pass parameter in laravel broadcastAs

hi i have this js code
var pusher = new Pusher('my pusher key', {
cluster: 'ap2'
});
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data)
{
console.log(data);
});
and this is my laravel code
protected $pos_invoice;
public function __construct($pos_invoice)
{
$this->pos_invoice = $pos_invoice;
}
public function broadcastOn()
{
return new Channel('my-channel');
}
public function broadcastAs()
{
return 'my-event';
}
and this is the call code
return event( new \App\Events\New_pos_online_order_event('aa'));
now the code
channel.bind('my-event', function(data)
{
console.log(data);
});
always return [] on console so i tried this
public function broadcastAs()
{
return 'my-event.'.$this->pos_invoice;
}
and this
public function broadcastOn()
{
return new Channel('my-channel'.'asdfasdf');
}
when i change anything on
public function broadcastOn()
{
return 'my-channel';
}
public function broadcastAs()
{
return 'my-event';
}
the code not working and not returning anything on console
so how can i pass parameter on pusher and laravel with js
thanks ..
You need to define the function broadcastWith
**
* Get the data to broadcast.
*
* #return array
*/
public function broadcastWith()
{
return ['pos_invoice' => $this->pos_invoice];
}
You will receive the array in the data of the bind function

At.js #mention - C# Web API

I am using https://github.com/ichord/At.js library to achieve autocomplete.
But it shows a list of "undefined" dropdown when I am using remoteFilter like they said in https://github.com/ichord/At.js/wiki/How-to-use-remoteFilter .
Model:
public class CaseHistory
{
public int CaseHistoryId { get; set; }
[Display(Name = "Symptom/Disease")]
[Required(ErrorMessage = "Please enter symptom or disease")]
public string SymptomOrDisease { get; set; }
public string Description { get; set; }
}
API action code:
private ApplicationDbContext db = new ApplicationDbContext();
// GET api/CaseHistories
public IQueryable<CaseHistory> GetCaseHistories()
{
return db.CaseHistories;
}
Here is my code in the razor view:
var myUrl = 'https://localhost:44301/api/CaseHistories';
$('#inputor').atwho({
at: ":",
callbacks: {
/*
It function is given, At.js will invoke it if local filter can not find any data
query [String] matched query
callback [Function] callback to render page.
*/
remoteFilter: function(query, callback) {
$.getJSON(myUrl, { q: query }, function (data) {
callback(data);
});
}
}
});
Change the code in the controller to be:
public dynamic GetCaseHistories()
{
return db.CaseHistories.Select(x => x.SymptomOrDisease).ToList();
}
The issue is that the parameter you pass to callback should be array of strings.
If you really wanted to do this in js:
var myUrl = 'https://localhost:44301/api/CaseHistories';
$('#inputor').atwho({
at: ":",
callbacks: {
/*
It function is given, At.js will invoke it if local filter can not find any data
query [String] matched query
callback [Function] callback to render page.
*/
remoteFilter: function(query, callback) {
$.getJSON(myUrl, { q: query }, function (data) {
var targetData = [];
for(var i = 0;i < data.length;i++){
targetData.push(data[i].SymptomOrDisease);
}
callback(targetData);
});
}
}
});

Dynamically calling a static method

In a class there are several static methods and the method to be called will be decided on the run time. How could I call this method dynamically?
export default class channel {
// METHOD THAT WILL DYNAMICALLY CALL OTHER STATIC METHODS
private static methodMap = {
'channel-create' : 'create',
'channel-user-count' : 'userCount',
'channel-close' : 'close'
};
public static do(commandType:string,params: any) {
if(channel.methodMap.hasOwnProperty(commandType)) {
// GET NAME OF THE METHOD
let method = channel.methodMap[commandType];
// CALL METHOD ON THE FLY
//return channel.call(method,params);
// channel.userCount(params);
}
}
/**
* Adds channel to available channel list
*/
private static create(channelName:string) {
}
/**
* Returns count of users in the channel
*/
private static userCount(channelName:string) {
}
}
You can dynamically invoke a method by using Classname['methodName'](param). As in your case, you can invoke create method as Channel['create']('MyChannel')
Here is the working example: Typescript Playground
class Channel {
private static methodMap = {
'channel-create' : 'create',
'channel-user-count' : 'userCount',
'channel-close' : 'close'
};
private static create(channelName:string) {
alert('Called with ' + channelName);
}
private static userCount(channelName:string) {
alert('Usercount called with ' + channelName);
}
public static do(commandType: string, params: any) {
if(Channel.methodMap.hasOwnProperty(commandType)) {
let method = Channel.methodMap[commandType];
return Channel[method](params);
}
}
}
Channel.do('channel-create', 'MyChannel');
Channel.do('channel-user-count', 1000);
Edit: Even though the above approach works, As #Ryan mentioned in his answer, providing functions directly in map is much cleaner.
private static methodMap: MethodMap = {
'channel-create': Channel.create,
'channel-user-count': Channel.userCount,
'channel-close': Channel.close,
};
Store the functions directly in the map:
type MethodMap = { [name: string]: (any) => void };
private static methodMap: MethodMap = {
'channel-create': Channel.create,
'channel-user-count': Channel.userCount,
'channel-close': Channel.close,
};
public static do(commandType: string, params: any) {
if (channel.methodMap.hasOwnProperty(commandType)) {
const method = channel.methodMap[commandType];
method(params);
}
}
To add to the answer by #HardikModha, you can also get the compiler to check the commandType against the possible values:
public static do(commandType: keyof typeof Channel.methodMap, params: any) {
if(Channel.methodMap.hasOwnProperty(commandType)) {
let method = Channel.methodMap[commandType];
return Channel[method](params);
}
}
...
Channel.do('channel-create', 'MyChannel'); // fine
Channel.do('channel-created', 'MyChannel'); // error
(code in playground)

How i can get return value from cordova plugin in Android?

I wrote a Java class that is a part of a cordova plugin, the main code is:
public class ClassName extends CordovaPlugin {
protected void pluginInitialize() {}
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("getData")) {
CallbackContext callback = null;
Test ts = new Test();
String result = ts.TestNow();
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
return true;
}
return false;
}
}
This is the js code of the plugin:
cordova.define("cordova-plugin-NAME.PLUGINNAME", function(require, exports, module) {
module.exports = {
getdata: function(message, successCallback) {
cordova.exec(successCallback, null, "ClassName", "getData", [message]);
}
};
});
And this is the js that i use to call plugin function:
function myFunc(){
alert('Function started');
ClassName.getdata(successCallback, null);
}
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('test').addEventListener('click', myFunc);
});
I have two questions:
1) My java class get in output a String result, how I can pass the result to my javascript function ( myfunc() )?
2) I don't understand what is the fucntion of "successCallback", could someone make me one example ?
successCallback in JS is executed when retrieving callbackContext.sendPluginResult(pluginResult) from JAVA.
The sucessCallback should look like:
successCallback:function(event){
.... Do things with the 'event' object received from JAVA
}
The "event" object is the object answered from JAVA.

Trying to pass an object to a spring controller using Jquery getJSON

I'm trying to pass a javascript object
var questionConstraintLineItem = {
"string1": "bhanu",
"string2": "prasad"
};
Using jQuery getJson
$.getJSON("/SafeSiteLive/common/createTaskWizard/saveTask.json", {
questionConstraintLineItem: questionConstraintLineItem
}, function (data)
{
try {
dialog.dialog("close");
getGroups();
} catch (e) {
alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message);
}
});
To my spring controller
#RequestMapping(value = "common/createTaskWizard/saveTask.json", method=RequestMethod.GET)
public #ResponseBody void saveTask(
QuestionConstraintLineItem questionConstraintLineItem) {
rest of the code...
I have tried using #RequestParam("questionConstraintLineItem") also.
Neither method works. Without the #RequestParam the request goes through to the server but the object is not filled with the data.
Am I doing this the wrong way?
Here is the QuestionConstaintLineItem POJO
public class QuestionConstraintLineItem implements Serializable{
private String string1;
private String string2;
public String getString1() {
return string1;
}
public void setString1(String string1) {
this.string1 = string1;
}
public String getString2() {
return string2;
}
public void setString2(String string2) {
this.string2 = string2;
}
}
Here is the error I get when I use the #RequestParam.

Categories

Resources