Using Titanium framework to create global array - javascript

I have three js controller file and 1 lib
app/lib/Client
function Client(id,name,blc){
this.id=id;
this.name=name;
this.blc=blc;
};
Client.prototype.getName = function(){
return this.id+' '+this.name+' '+this.blc;
};
Client.prototype.withdraw = function(amount){
if(amount<0) return -1;
if(this.blc<amount) return -1;
return this.blc-=amount;
};
Client.prototype.deposite = function(amount){
if(amount<0) return -1;
return this.blc+=amount;
};
module.exports = Client;
app/controller/addClient //this is where I want to add to global array
var args = arguments[0] || {};
var Client = require('Client');
function doClick(e) {
var user_id = $.id.getValue();
var user_name = $.name.getValue();
var user_blc = $.Balance.getValue();
if(user_id.length<=0 && user_name.length<=0 && user_blc.length<=0){
alert('you entred an invalid information');
}
else{
var c = new Client(user_id,user_name,user_blc);
alert(c.getName()+' is add successfly');
//note!!!!!
//add a global array to save the data
$.id.setValue("");
$.name.setValue("");
$.Balance.setValue("");
}
}
$.addClient.open();
app/controller/allClient
//this is where i want to use the data in Global app to make a listView

If you really want a "global array" use Alloy.Globals
If you want a collection of models, use Backbone

Related

Frida - Calling a specific method overload

I am exploring an Android program that has several methods with the same names and parameters.
I need to call a specific method overload.
Java code like this
package a;
public class d
{
public int a() {
return 10;
}
public long a() {
return 20;
}
public long b() {
long ret = a();
return ret + 1;
}
}
I need replace implementation of b() and call (int)a() instead of (long)a().
Please help me fix my frida js code.
Java.perform(function () {
Var Class_A_D = java.Use("a.d");
Class_A_D.b.implementation = function(){
var ret = this.(a); // need to call int implementation
return ret;
}
}
I found the answer with Java.reflect
Java.perform(function () {
var Java_lang_Object = Java.use('java.lang.Object');
var Java_lang_String = Java.use('java.lang.String');
//This function get method reference by reflect
function dynamic_search_method(io_object, iv_name, iv_ret_type, it_par){
var lt_methods = io_object.getMethods() ;
var lv_found;
for(var lv_i=0;lv_i < lt_methods.length;lv_i++){
if(lt_methods[lv_i].getName().toString() == iv_name && lt_methods[lv_i].getGenericReturnType().toString() == iv_ret_type){
var lt_par_type = lt_methods[lv_i].getParameterTypes();
if(lt_par_type.length == it_par.length){
lv_found = true;
for(var lv_j=0;lv_j < lt_par_type.length && lv_found == true;lv_j++){
if(lt_par_type[lv_j].getName().toString() != it_par[lv_j]) lv_found = false ;
}
if(lv_found == true) return lt_methods[lv_i];
}
}
}
return null;
}
//This function call method dynamically
function dynamic_invoke(io_object,io_method, it_par){
if(io_object===null || io_method ===null ) return null;
try{
var lo_cast_obj = Java.cast( io_object ,Java_lang_Object);
}catch(e){
return null;
}
var lt_par = Java.array('java.lang.Object',it_par);
return io_method.invoke(lo_cast_obj,lt_par);
}
//example of use
Var Class_A_D = java.Use("a.d");
Class_A_D.b.implementation = function(){
// call method "a" of instance, with "long" return and without parameter
var lo_meth = dynamic_search_method(this.getClass(),"a","long",[]);
var lv_var= dynamic_invoke(this,lo_meth,[]);
// call method "c" of instance, with "android.content.Context" return and with 2 parameters
lo_meth = dynamic_search_method(this.getClass(),"a","class android.content.Context",['java.lang.String','java.lang.String']);
var lv_var= dynamic_invoke(this,lo_meth,[Java_lang_String.$new("Test"),Java_lang_String.$new("String")]);
// call static method
var lo_meth = dynamic_search_method(Class_A_D.class,"d","class java.lang.String",[]);
var lv_var= dynamic_invoke(Class_A_D.class,lo_meth,[]);
};
)};
The code in class d is violating the method signature principle which is defined by the method name and each parameter type.
Because the method type is not part of the signature Java reads these methods as the same:
public int a() {...}
public long a(){...}
both signatures are "a()"
You should rename those methods to have different names ( or to have input parameters of different types )
More info here : https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

How to make local varible global javascript

I have this class like so :
https://jsfiddle.net/0sh7fLtp/
When I create a new object of this class, my local variable can't be seen even when I assign to window in the class:
function Hilitor() {
var excat;
this.setMatchType = function(type) {
if (type == "exact"){
window.excat = true;
}
};
this.setRegex = function(input) {
alert(excat);
};
this.apply = function(input) {
this.setRegex();
};
}
and this is how i call it :
var myHilitor = new Hilitor();
myHilitor.apply();
myHilitor.setMatchType("exact");
Not sure I completely understand your question but you are trying to compare a variable "excat" to string "excat"... See this fiddle to how you can make your var a string and then get desired output..
https://jsfiddle.net/shemdani/0sh7fLtp/5/
var myHilitor = new Hilitor();
myHilitor.setMatchType("excat");
myHilitor.apply();
function Hilitor()
{
var excat;
this.setMatchType = function(type)
{
if(type == "excat"){window.excat = true;}
};
this.setRegex = function(input)
{
alert(window.excat);
};
this.apply = function(input)
{
this.setRegex();
};
}
Two main problems
1) Your var exact inside the function is not a global variable and so not accessible on the window object. (But that's a good thing).
Your code will work if you remove window.exact for just exact
this.setMatchType = function(type)
{
if(type == "exact"){excat = true;}
};
2) You are also calling apply before you call setMatchType. Switching them like this works:
var myHilitor = new Hilitor();
myHilitor.setMatchType("excat");
myHilitor.apply();
Working example

Access a parent's member

Given the following code:
self.pcsList = ko.observableArray(ko.utils.arrayMap(pcs, function (pc) {
return {
obCurHp: ko.observable(pc.curHp), obMaxHp: ko.observable(pc.hp),
obHpPerc: ko.computed(function(){return Math.round(this.obCurHp() / this.obMaxHp())*100 + "%";})
};
}));
obHpPrec does not evaluate to anything because neither this.obCurHp() & this.obMaxHp() are a thing nor are obCurHp() & obMaxHp().
I need to access these members of the current pcsList object so I can construct the computed object. How would I go about doing this?
You need to keep this in each context separate. You can have a sub model and just create a new instance for each element.
var mainViewModel = function (){
var self = this;
self.pcsList = ko.observableArray(ko.utils.arrayMap(pcs, function (pc) {
return new pcListItemViewModel(pc);
}));
}
var pcListItemViewModel = function (pc){
var self = this;
self.obCurHp = ko.observable(pc.curHp);
self.obMaxHp = ko.observable(pc.hp);
self.obHpPerc = ko.computed(function(){
return Math.round(self.obCurHp() / self.obMaxHp())*100 + "%";
});
}

Custom script in Meteor

I just started playing with Meteor and i was testing few things. Based on the Meteor Guide i was testing below code to understand the best way to write packages but i never been successful. What is wrong with the following code and what is good way (if not all but atleast few good ways) to write the packages for Meteor app which can be placed in lib folder.
/lib/exports.js
if (org === void 0){
var org = {}
}
if(bjse === void 0){
var bjse = {};
if(typeof exports != "undefined"){
bjse = exports;
}
bjse.api = {};
}
/lib/file1.js
// mypackage.js
bjse.api.Whirlygig = function (name) {
var self = this;
self.name = name; // name of the remote weasel
self.values = {}; // remote key name -> 0-indexed value
};
_.extend(Whirlygig.prototype, {
// Take a key/value pair from the remote Weasel and save it locally.
addValue: function (x) {
// Weasels use 1-indexed arrays. Subtract 1 to convert to 0-indexed.
self.values[x.key] = x.value - 1;
},
// Return a list of stored values in a format suitable for sending to
// a Weasel.
serialize: function () {
return _.map(self.values, function (v, k) {
var newVal = mungeValue(v, false /* foldValue */);
// Weasels use 1-indexed arrays. Add 1 to convert back to 1-indexed.
newVal = newVal + 1;
return {key: k, value: newVal};
});
}
});
/server/methods.js
Meteor.methods({
createConnections: function(){
....
var serializeObj = bjse.api.Whirlygig.serialize(..);
But i get Whirlygig not defined.
Update
I want to use bjse as namespace but it always appears as not defined in other files.
In exports you want:
if (typeof(org) === 'undefined'){
var org = {}
}
if(typeof(bjse) === 'undefined'){
var bjse = {};
if(typeof exports != "undefined"){
bjse = exports;
}
bjse.api = {};
}

How to declare and access global variable

What Im doing wrong in the below code?
//File1.js
var arr = [];
function insertName {
var name = "josh";
arr.push(name);
return name;
};
function validName(key) {
var index = arr.indexOf(key);
if (index == -1) {
return false;
} else {
return true;
}
}
var result = insertname();
exports.arr = arr;
exports.validName = validName;
//File2.js
var file1 = require("./File1.js");
var name = "josh";
var verify = file1.validName(name);
if(verify) {
cosnole.log("Valid name");
}else {
console.log("Error");
}
node File1.js
node File2.js
When Im executing File2.js, Im gettin undefined for arr[]. Can someone help me what Im doing in the below code
Node.js modules retain the variables you declared at their top level, till the module is Garbage Collected or you manually delete them. If you look at your File1.js, you are exporting the array object, nothing else. So when you say
var file1 = require("./File1.js");
file1 is just a reference to a JavaScript object which has an arr property. You can check this by printing the file1. The functions you created in File1 are never exported. So, you can fix it like this*
exports = module.exports = {
validName: validName,
insertName: insertName
}
Now, you are exporting the functions and they can still access the arr variable. From File2, you can invoke insertName like this
file1.insertName();
if (file1.validName("josh")) {
console.log("Valid name");
} else {
console.log("Error");
}
* To know more about exports and module.exports, you can check my blog post about this
Your code contains mistakes change your code like below
File1.js
var arr = [];
function insertName() {
var name = "josh";
arr.push(name);
return name;
};
function validName(key) {
var index = arr.indexOf(key);
if (index == -1) {
return false;
} else {
return true;
}
}
var result = insertName();
exports.validName = validName;
File2.js
var file1 = require("./File1.js");
var name = "josh";
var verify = file1.validName(name);
if(verify) {
console.log("Valid name");
} else {
console.log("Error");
}
You can simply use global.(name) = (value)
Example :
main.js
global.foo = 1;
require('./mod.js').show();
mod.js
module.exports = {
show : function(){
console.log(global.foo); // which prints "1"
}
}

Categories