SAPUI5 jQuery.sap.storage variable saving - javascript

In my application I have a function for a synchronisation in which I take two timestamps, at the start and the end, to get the time spent while synchronizing.
I want to save this variable into the local storage.
After that I need to compare the variable coming from the function with the variable from the function and get the average of them.
I know the storage is a key-value type, I still have problems getting this work. The function is posted below. Thanks for every possible help.
handleSyncPress: function() {
new Date().getTime();
var syncStart = Math.floor(Date.now() / 1000);
var that = this;
var fUpdateBindings = function() {
that.getView().getModel().refresh(true);
}
test.mp.Offline.sync(fUpdateBindings);
new Date().getTime();
var syncEnd = Math.floor(Date.now() / 1000);
var syncTime = syncEnd - syncStart;
this._oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);
this._oMyData = this._oStorage.get(syncTime);
this._oStorage.put(syncTime, this._oMyData);
}
As you can see I'll started the initialisation of the storage already at least.

As I said in a comment in your other question, the storage is something like a dictionary that stores key-value-pairs.
The key is the identifier that you will use to access your value later.
The value can be anything: Numbers, Strings, Bools, Arrays, Objects, you name it.
Imo the best solution would be to store all your sync times in a single value (i. e. an array of sync times).
handleSyncPress: function() {
// get current timestamp
var syncStart = Date.now();
// do stuff
var fUpdateBindings = function() {
that.getView().getModel().refresh(true);
}
test.mp.Offline.sync(fUpdateBindings);
// get another timestamp
var syncEnd = Date.now();
// diff between the timestamps is the sync time (in milliseconds)
var syncTimeInMilliseconds = syncEnd - syncStart;
this._oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);
// load value for the key "syncTimes"
var aSyncTimes = this._oStorage.get("syncTimes");
aSyncTimes = JSON.parse(aSyncTimes); // may not be needed
// if this is the first time you access the key, initialize the value
if (aSyncTimes === null) {
aSyncTimes = [];
}
// append your new sync time
aSyncTimes.push(syncTimeInMilliseconds);
// store your sync time array
aSyncTimes = JSON.stringify(aSyncTimes); // may not be needed
this._oStorage.put("syncTimes", aSyncTimes);
// hopefully you already know how to calculate the avg value from an array of integers
// if not: avg = sum / length
}
Edit: According to the API, only strings are supported as values. I tried other types, and they worked, but it might be safest to (de)serialize the data. I updated the code example.

The line
this._oMyData =this._oStorage.get(syncTime);
will return nothing in your case, right? This is because you did not store a value before this call. Furthermore, I guess you should use a String for key...
Using SAPUI5 for accessing the localStorage would work like this:
// get an instance of jQuery.sap.storage.Storage
var oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);
//...
// Store
var syncTime = ...;
oStorage.put("syncTime", syncTime);
// Read
var syncTime = oStorage.get("syncTime");
However, I prefer to use native JavaScript APIs, i.e. see http://www.w3schools.com/html/html5_webstorage.asp:
// Store
var syncTime = ...;
localStorage.setItem("syncTime", syncTime);
// read
var syncTime = localStorage.getItem("syncTime");
The key should be string...

Related

excel-JS excel left() worksheet function not working

When I am trying to use the Excel left() worksheet function in visual studio Excel-JS api, it does not work at all! this means there is no error message, there is no value and not a clue what is not working
I am talking about this statement:
onzin = ctx.workbook.functions.left(Acode.values.toString(), 7);
onzin.load("values","address");
According to the (incredibly incomplete) documentation from Microsoft https://learn.microsoft.com/en-us/javascript/api/excel/excel.worksheetcollection?view=office-js
you have to load the variable in the Excel Object model before you use it and after a synchronization of the context, the values are available. But in my 2nd promise I loaded the values and the address of "onzin" but when I want to assign the values to the ICcode Range, it does not do it and furthermore, when I put a breakpoint in it, it seams to be empty and no errors.
Here is my code:
Excel.run(function (ctx) {
//always use the Data sheet
var MyDataSheet = ctx.workbook.worksheets.getItem("Data");
var ConfigSheet = ctx.workbook.worksheets.getItem("Config");
var onzin;
MyDataSheet.activate();
var productStartRange = MyDataSheet.getUsedRange();
//define a range before the values can be loaded to the Excel Object model
var Acode = MyDataSheet.getRange("A3").load("values, address");
productStartRange.load("values, address, length");
// Run the queued-up commands, and return a promise to indicate task completion
return ctx.sync()
.then(function () {
var myBounds = GetBounds(productStartRange);
ConfigSheet.activate();
//put the column and row bounds in the config sheet
var ColBounds = ConfigSheet.getRange("B22");
var RowBounds = ConfigSheet.getRange("B21");
var NumProducts = ConfigSheet.getRange("B34");
var NumProperties = ConfigSheet.getRange("B27");
//ICcode.values = ctx.workbook.functions.left(Acode.values.toString(), 7);
onzin = ctx.workbook.functions.left(Acode.values.toString(), 7);
onzin.load("values","address");
//ICcode.values = Acode.values.toString().substring(0, 7);
//ICcode.values = onzin.values;
ColBounds.values = myBounds.LastCol;
RowBounds.values = myBounds.LastRow - 1;
//total number of products
NumProducts.values = RowBounds.values - 2;
//total number of properties
NumProperties.values = ColBounds.values - 2;
//load the products from the Data source sheet into one range
var ProductRange = MyDataSheet.getRangeByIndexes(3, 1, myBounds.LastRow, 3);
ProductRange.load("values");
})
.then(ctx.sync)
.then(function () {
var ICcode = ConfigSheet.getRange("B36");
ICcode = onzin.values;
//var Mystring = rowAddress.address;
showNotification("onzin waardes: ", onzin.values);
var PropSheet = ctx.workbook.worksheets.getItem("PropertySelection");
PropSheet.activate();
});
}).catch(errorHandler);
I would expect that the worksheet function takes the first 7 characters from the cell value of "Acode" and writes it to the range ICcode on location B36.
any help would be appreciated
Deleted my earlier answer because I misread part of your code.
This line of your code has two problems:
onzin.load("values","address");
There should not be an "s" on the first string. It is just "value". Also, remove the "s" from the line ICcode = onzin.values;.
There is no "address" property on onzin object. (The Excel.Range object does have properties named values and address, which is why I thought in my original answer that you were treating onzin as an Excel.Range object.)
I was able to spot the errors by reading this article in the official documentation: Call built-in Excel worksheet functions. This article is the very first search result in both Bing and Google if you search for "office add-ins worksheet functions". So, I gently don't agree with you that the documentation is "incredibly incomplete".
Will you try this piece, it works on my side.
var Acode = sheet.getRange("B2").load("values, address");
await context.sync();
console.log(Acode);
var onzin = context.workbook.functions.left(Acode.values.toString(), 3);
var substring = Acode.values.toString().substring(0,7);
console.log(Acode.values.toString());
console.log(substring);

Javascript search if LocalStorage variable exists

I am programming a chat system. I always make a Localstorage variable when a new chat is opened. Created like this:
localStorage.setItem("chat_"+varemail, data);
Now i want to check how many of them I have so something like:
"chat_"+... count.
How can I do this?
You'd grab the array of keys of the localStorage object, and use Array.filter to grab only the items starting with "chat_":
var length = Object.keys(localStorage).filter(function(key) {
return /^chat_.+/.test(key);
}).length;
Here's a JSFiddle
Try something like this, loop through all items in localStorage and match against your pattern
function getChatCount(){
var chatCount = 0;
for(item in localStorage){
if(item.indexOf('chat_') > -1) chatCount++;
}
return chatCount;
}
Local storage is based on key, value pairs. AFAIK, you wouldn't be able to retrieve all values with a certain prefix.
One potential solution would be to store an object that contains these. Based on your needs you could store the objects in an array or object and then retrieve the entire set and find the count.
For example:
var chats = { count: 0 };
chats["chat_"+varemail] = data;
chats.count += 1;
localStorage.setItem('chats', data);
Then if you want a count, you would retrieve the object:
var chats = localStorage.getItem('chats');
//chats.count would give you the count.
Although, this would mean you have to manually maintain the count variable when adding or removing data. If you don't need the indexing ability, you could add the chats to an array and store that.
EDIT: It is possible to find properties with a certain prefix, as seen in an answer to this question.
I would offer to convert to localStorage.setItem("chat", JSON.stringify(stack)), where stack is an array of chat objects. This way, you have access to an array of chats which you can count, search within, etc.
Something like:
var chatStore =
{
Count: function () {
var stack = JSON.parse(localStorage.getItem("chats"));
if (!stack)
return 0;
return stack.length;
},
Peek: function () {
var stack = JSON.parse(localStorage.getItem("chats"));
if (!stack)
stack = [];
if (stack.length > 0)
return stack.pop();
},
Push: function (token) {
var stack = JSON.parse(localStorage.getItem("chats"));
if (!stack)
stack = [];
stack.push(token);
localStorage.setItem("chats", JSON.stringify(stack));
},
// more methods to you might wish to implement: search, insert, etc.
}
// usage:
chatStore.Push(chatObject); // sets the last chat
chatStore.Peek(); // gets the last chat

count the number of rows in 2d array

Description:
I am adding the data in my 2D array like the following
my_2d_array['user1'] = {'id':name,'socket':socket};
my_2d_array['user2'] = {'id':name,'socket':socket};
This 2D array keeps the record of all the connected users id and their respective sockets ...
I want to display the number of users connected .. So far to do that would be to count the number of rows in the array and display it
I have tried following:
my_2d_array[].length; // this gives nothing
my_2d_array.length; // this outputs 0 (as number)
What should I do to get the number of rows
UPDATE
I declared my array like this
var my_2d_array = [];
This could work for you
// initialize
var my_2d_array = {};
// add users
my_2d_array["user1"] = ...
my_2d_array["user2"] = ...
// get number of users
Object.keys(my_2d_array).length;
//=> 2
You should consider using users instead of my_2d_array though. It communicates better and the actual data type is an Object, not specifically an Array.
Use push method
my_2d_array.push({'id':name,'socket':socket});
my_2d_array.push({'id':name,'socket':socket});
And my_2d_array.length to get the count
It looks like you are trying to figure out how many keys are in your javascript object my_2d_array.
You should be able to use Object.keys()
Here is a JsFiddle.
var my_2d_array = {};
var name = "Hello";
var socket = "World";
my_2d_array['user1'] = {'id':name,'socket':socket};
var name = "Hello2";
var socket = "World2";
my_2d_array['user2'] = {'id':name,'socket':socket};
alert( Object.keys(my_2d_array).length );

Numerically ordered ID's of data objects in Firebase

I am pretty new to the 'game' and was wondering if it's possible to order newly added data (through a form and inputs) to the Firebase numerically so each new data entry gets the ID (number of the last added data +1).
To make it more clear, underneath you can find a screenshot of how data is currently being added right now. The datapoint 0-7 are existing (JSON imported data) and the ones with the randomly created ID belong to new entries. I would like to have the entries to comply to the numbering inside of my Firebase, because otherwise my D3 bar chart won't be visualised.
var firebaseData = new Firebase("https://assignment5.firebaseio.com");
function funct1(evt)
{
var gameName = $('#nameInput').val();
var medalCount = $('#medalsInput').val();
var bool = $('#boolInput').is(':checked');
firebaseData.push().set({games: gameName, medals: medalCount, summergames: bool});
evt.preventDefault();
}
var submit = document.getElementsByTagName('button')[0];
submit.onclick = funct1;
UPDATE:
function funct1(evt)
{
var gameName = $('#nameInput').val();
var medalCount = $('#medalsInput').val();
var bool = $('#boolInput').is(':checked');
for (var i = 0; i < 10; i++)
{
firebaseData.child('7' + i).set({games: gameName, medals: medalCount, summergames: bool}(i)); };
Problem:
There are two ways to generate ids for your document nodes.
Calling .push() on your reference will generate that unique id.
Calling .set() on your reference will allow you to use your own
id.
Right now you're using .push().set({}), so push will generate an new id and the set will simply set the data.
// These two methods are equivalent
listRef.push().set({user_id: 'wilma', text: 'Hello'});
listRef.push({user_id: 'wilma', text: 'Hello'});
Using .set() without .push() will allow you to control your own id.
Using .push():
When managing lists of data in Firebase, it's important to use unique generated IDs since the data is updating in real time. If integer ids are being used data can be easily overwritten.
Just because you have an unique id, doesn't mean you can't query through your data by your ids. You can loop through a parent reference and get all of the child references as well.
var listRef = new Firebase('https://YOUR-FIREBASE.firebaseio.com/items');
// constructor for item
function Item(id) {
this.id = id;
};
// add the items to firebase
for (var i = 0; i < 10; i++) {
listRef.push(new Item(i));
};
// This will generate the following structure
// - items
// - LGAJlkejagae
// - id: 0
// now we can loop through all of the items
listRef.once('value', function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var name = childSnapshot.name();
var childData = childSnapshot.val();
console.log(name); // unique id
console.log(childData); // actual data
console.log(childData.id); // this is the id you're looking for
});
});
Within the childData variable you can access your data such as the id you want.
Using .set()
If you want to manage your own ids you can use set, but you need to change the child reference as you add items.
for (var i = 0; i < 10; i++) {
// Now this will create an item with the id number
// ex: https://YOUR-FIREBASE.firebaseio.com/items/1
listRef.child('/' + i).set(new Item(i));
};
// The above loop with create the following structure.
// - items
// - 0
// - id: 0
To get the data you can use the same method above to loop through all of the child items in the node.
So which one to use?
Use .push() when you don't want your data to be easily overwritten.
Use .set() when your id is really, really important to you and you don't care about your data being easily overwritten.
EDIT
The problem you're having is that you need to know the total amount of items in the list. This feature is not implemented in Firebase so you'll need to load the data and grab the number of items. I'd recommend doing this when the page loads and caching that count if you really desire to maintain that id structure. This will cause performance issues.
However, if you know what you need to index off of, or don't care to overwrite your index I wouldn't load the data from firebase.
In your case your code would look something like this:
// this variable will store all your data, try to not put it in global scope
var firebaseData = new Firebase('your-firebase-url/data');
var allData = null;
// if you don't need to load the data then just use this variable to increment
var allDataCount = 0;
// be wary since this is an async call, it may not be available for your
// function below. Look into using a deferred instead.
firebaseData.once('value', function(snapshot) {
allData = snapshot.val();
allDataCount = snapshot.numChildren(); // this is the index to increment off of
});
// assuming this is some click event that adds the data it should
function funct1(evt) {
var gameName = $('#nameInput').val();
var medalCount = $('#medalsInput').val();
var bool = $('#boolInput').is(':checked');
firebaseData.child('/' + allDataCount).set({
games: gameName,
medals: medalCount,
summergames: bool
});
allDataCount += 1; // increment since we still don't have the reference
};
For more information about managing lists in Firebase, there's a good article in the Firebase API Docs. https://www.firebase.com/docs/managing-lists.html

Issue with JSON stringify?

/* Helper function to clean up any current data we have stored */
function insertSerializedData(ids, type) {
// Get anything in the current field
current_data = $('#changes').val();
if (!current_data) {
var data = new Array();
data[type] = ids;
$('#changes').val(JSON.stringify(data));
} else {
var data = JSON.parse($('#changes').val());
data[type] = ids;
$('#changes').val(JSON.stringify(data));
}
console.log($('#changes').val());
}
I am using the following function to either add data to a current JSON object or create a new JSON object all together to be used in PHP later. Is the stringify() method only for FF? I am using google chrome and I am being given an empty object when using the conosole.log() function...
Also what happens if you try to store two values with the same key? I assume it will overwrite...so I should add a random math number at the end array in order to keep duplicates from showing up?
Thanks :)
These lines may cause problems:
var data = new Array();
data[type] = ids;
... because arrays in JavaScript are not quite like arrays in PHP. I suppose what you meant is better expressed by...
var data = {};
data[type] = ids;
Besides, current_data seems to be local to this function, therefore it also should be declared as local with var. Don't see any other problems... except that similar functionality is already implemented in jQuery .data() method.
UPDATE: here's jsFiddle to play with. ) From what I've tried looks like the array-object mismatch is what actually caused that Chrome behavior.
I reformatted it a bit, but and this seems to work. It will set the "value" attribute of the #changes element to a JSON string. I assume that the type argument is supposed to be the index of the array which you're trying to assign?
function insertSerializedData(ids, type) {
var changes = jQuery('#changes'), arr, val = changes.val();
if (!val) {
arr = [];
arr[type] = ids;
changes.val(JSON.stringify(arr));
} else {
arr = JSON.parse(val);
arr[type] = ids;
changes.val(JSON.stringify(arr));
}
console.log(changes);
}

Categories

Resources