can I use indexDB to store sipml5 client objects - javascript

regarding this problem:
Call get disconnected while I am refreshing the SIPML5 demo page .
can be found here
https://groups.google.com/forum/#!msg/doubango/BlAww-8Wq4U/79Rupoa4BwAJ;context-place=searchin/doubango/page$20refresh%7Csort:date
I am searching for a solution to keep the call going even if the client page get refreshed
I know that all variables lives inside the javascript file will be re-created when a page get refreshed but my question is :
can I use indexDB to store all the objects that sipml5 client use so the call never get disconnected on page refresh?

Yes, you can store and retrieve sipml5 client objects from and to IndexedDB since it can store any type of objects and use Structured Cloning Algorithm to serialize the data. Basically it can save all javascript data types in plain object, in nested or in circular reference.
The structured clone algorithm is an algorithm defined by the HTML5 specification for copying complex JavaScript objects. It is used internally when transferring data to and from Workers via postMessage() or when storing objects with IndexedDB. It builds up a clone by recursing through the input object while maintaining a map of previously visited references in order to avoid infinitely traversing cycles. You can get more information from here

Related

firebase - get child database reference with key

I am using Firebase for my web app and fairly new with it.
As I read the documentation, it mentioned that in order to minimize the size of download (for cost saving reason), we should flatten the data structure so that we will not download unnecessary data. But throughout the doc, they will always reference the whole database first:
dbRef = firebase.database().ref();
then only sample the data with key:
childRef = dbRef.child(child_key)
I am not sure I got this right but at least this is how I understand it.
My question is, won't that dbRef already taken the whole database down? Or the download only happen during the childRef in the scenario above?
Any information will be helpful as I google and found some nightmare cases with unbelievable price because of this database issue that is not handle correctly.
Is there other issue that I need to worry about now since I am at the beginning stage of development?
A Reference that you get from ref() and child() is just a pointer into a location in the database. It's exceptionally cheap, and creating one doesn't perform any data access.
If you want to actually fetch the data from a reference, you have to call on() or once() on it. Until then, all you have is a tiny object that contains a location. Same thing with Query objects. They don't perform any queries until you call one of those same methods.
There is a difference between a database ref and the actual database data request ( on('value') and once('value'))
The database reference is representing a particular location (or child/node/ref) in your database.
The moment you call one of these methods on the Reference object (child() also returns a Reference object) you are actually fetching data, which is the expensive part.
Besides that, it's always a good thing to have just one variable thats holding a reference.

Uses of serialization in a web application

I'm teaching JS my self and I just been reading about objects JavaScript Objects in Detail which also discuss object serialization. So I tired to read more about that in order to understand the concept and to find a real use. So I found many tutorials on how to use JSON.stringify() and JSON.parse(), but I still can't come up with a scenario when I would need to serialize an objecct. The only one scenario I've used so far was to serialize a from with jQuery in order to sent data with .post(), but I'm not to sure if this is related.
What are the most common scenarios for serializing an object in a web application?
There are lot of scenarios, the most useful I can think of is when you want to store the data offline ( for example mobile or desktop application), you can use localStorage to store the serialized data (using JSON.stringify()) and load it back by parsing it into object (using JSON.parse()).
This offline storing of data is useful when the user don't have internet access and the user still wants to see already fetched items.
Note:
You can find more info at below references:
localStorage Reference
How to use local storage

Saving a game with Javascript

Suppose I have a Javascript game that draws on a HTML5 canvas, it loops through updating and drawing a bunch of objects. What's the best way to go about saving the current state of the game? I guess I could loop through all the objects and write all values in text form in a format that can be read by a loading function, but that seems like quite a lot of work for both me and the game, as well as a large save file size. Is there a better way to do this? I've never done something like that before, I don't really know where to start.
Thanks.
A standard practice for something like this is to maintain a data model which represents your system, and a view that represents your model. The model should be mostly data, which can be relatively complex, but it has all the information necessary for your view to render.
Assuming this model is an object tree, you can just call var serialized = JSON.stringify(model) to serialize your data model to a string and then var model = JSON.parse(serialized) to get it back. This is a very simple, yet inflexible approach, but it is a start.
A quick way to store this information would be with localStorage but you will likely want to move to a back-end storage where you are putting values up to a REST service (or something like that) so that this data can be retrieved from anywhere.
I recommend looking into MVC-like frameworks like Backbone.js (for instance) which helps you to separate your models from your views, and also does a great job of persisting/hydrating your data from your data store.
You'll want to do some research on the keyterms of 'serialize' and 'unserialize'.
Instead of doing a recursive loop on every object, I'd suggest that you have all your game objects implement a function for serialization. You can use JSON to build up your entire object tree, with each object deciding what properties need to be kept, and which properties can be derived from the rest. While this does appear to do the same thing as a recursive loops on some levels, the huge difference is that each object can define, on it's own, how to serialize/unserialize itself and it's contents..
Depending no how precisely you want to save your game state, you can reduce a LOT of data that way. For example, maybe you can simply reset them to the beginning of the current level, or you could dump some data that's available elsewhere (level data, for example, probably shouldn't be stored in the save file, only the information on game state that lets you reproduce the level using your existing level data store).

Google apps script: how to persist data in spreadsheet between different function calls?

In a Google spreadsheet using the Script Editor, I do function calls, but I am not quite sure if the best way to store persistant data (data that I will continue to use) is to use global variables (using objects, arrays, strings), or there is a better way to store data.
I don't want to use cells which could be another way.
Another question, is it possible to create (pseudo) classes in this environment? Best way?
Both ScriptProperties and ScriptDB are deprecated.
Instead, you should be using the new class PropertiesService which is split into three sections of narrowing scope:
Document - Gets a property store that all users can access within the current document, if the script is published as an add-on.
Script - Gets a property store that all users can access, but only within this script.
User - Gets a property store that only the current user can access, and only within this script.
Here's an example persisting a user property across calls:
var properties = PropertiesService.getScriptProperties();
function saveValue(lastDate) {
properties.setProperty('lastCalled', lastDate);
}
function getValue() {
return properties.getProperty('lastCalled');
}
The script execution environment is stateless, so you cannot access local variables from previous runs, but you can store getScriptProperties() in a local variable because it will be re-run for each return trip to the server so it can be called in either method.
If you need to store something on a more temporary basis, you can use the CacheService API
Persistent data can be stored using the Class ScriptProperties:
http://code.google.com/googleapps/appsscript/class_scriptproperties.html
All values are stored as a string and will have to be converted back with the likes or parsInt or parseFloat when they are retrieved.
JSON objects can also be stored in this manner.
My experience has been that every query to retrieve or store values takes a long time. At the very least, I would cache the information in your javascript code as much as possible when it is safe. My scripts always execute all at once, so I don't need to keep global variables as I simply pass the retrieved data arrays around, manipulate them, and finally store them back in one fell swoop. If I needed persistence across script invocations and I didn't care about dropping intermediate values on close of the webpage, then I'd use globals. Clearly you have to think about what happens if your script is stopped in the middle and you haven't yet stored the values back to Google.

saving/loading settings in a SenchaTouch/PhoneGap -App with localStorage and display them in a form

How you should save/load settings in an app when you want to use the localStorage?
Let's say we just want to save a username and a password.
Currently I know two options:
using a model with store and proxy.
This seems to be a bit too "big" as you only want to store one object. But you have the advantage to easily use this.load() in the settings FormPanel.
using the Phonegap or the 'plain' localStorage.
Then you have to use JSON.stringify and JSON.parse but you store only one object and its easier to load it (after a page refresh I run into some trouble using the store and proxy).
But you can't use this.load as you need to load a model. Maybe you need to "cast" the settingsObject from the localStorage to a model and load this one!?
Which approach is better or is there even another, better approach to save/load settings and display them in a form?
I definitely agree with you that using a Sencha Touch model and store is overkill to store some settings. I recommend serializing your settings object with Ext.util.JSON.encode/decode.
Note: You won't be able to serialize the function members of an object though, much less the properties belonging to objects up its prototype chain, so you won't be able to serialize and deserialize an Ext model and have it work when you get it back. The best you could do is serialize the configuration for your Ext component and reconstitute it through its constructor after getting the configuration back.
For most simple settings objects, just serialize it and store it in a key in the local store. When you get to your page, get the settings object out and update your form. When you change a setting in the form, write it to the object. When you leave the page (onbeforeunload), write the object to localstore.

Categories

Resources