I'm using a third party javascript library in my application and I need to be able to monitor a particular array property for changes. So for example, the third party library adds an object to widget.loadingQueue anytime a widget is being loaded. I want to be able to monitor this property and display a loading indicator anytime there are elements in the array. The library is open source, but if possible I'd like to do this without having to go through their source and find every time they add something to the array and add in a method call.
If they add to the array by calling push(), you can simply create a custom push() method on the array that runs your handling code and calls Array.prototype.push().
This is how Google Analytics' _gaq array works.
Related
I am trying to make an user Inventory using React js and firestore.
My document has a field for "items" which has an array. I want to add items into the array through updateDoc() but I don't know how to add the data without overwriting(erasing) the already existing items in the array.
I wish to add more properties to the items, which would make my structure look like:
User>Username>items>item>value
Or maybe I should use collections for the items? I plan to create a User for each new player and they will have a bunch of items and a bunch of properties. So I would appreciate all kinds of performance suggestion.
There's a method in Firestore library (doc for v8) called firebase.firestore.FieldValue.arrayUnion() where you can add more items to an array. It works with set() and update().
Here you have some examples in the official docs.
Another simpler method if you wish to start easier, you could retrieve the document, handle the push of the new object in your javascript/React app, and set the document again. To try at first it's fine, but I'll definitely recommend the first solution if you want to stick with arrays.
In Struts2 I have successfully implemented an updateErrorCount() method that updates a class member variable every time addActionError() is called through out various action classes. I can then access that variable with a property tag in the associated jsp. However, I am looking for a better solution.
Since the s:actionerror tag lists all of the errors added, is there a way to use an iterator tag or some other solution to display the error count along with all of the error messages right in the jsp?
I thought about overriding addActionError() to avoid having to call an additional method to keep an error count but if I could simply do it all in the jsp it seems much cleaner.
getActionErrors returns a collection, why can't you just call size on that?
I see few legitimate reasons to implement any of this functionality manually.
If you have specific needs you should enumerate them in your question, otherwise I don't see the point of doing this on your own.
I have a page that uses Ajax to load in sections of a page.
Each section consists of a html files and a JavaScript file that defines events for just that bit of html.
What I am trying to do is figure a method of managing the file loading that can bind keep the two files bound together.
Here is pseudo-code of how I am attempting to make it work now but I don't know if there is a more organized way.
Pseudo-code: (using jQuery)
Cycle through list of html/js file pairs that need loaded.
Add an object to the an array that uniquely identifies the pair of files. This object will eventually hold the container for the html and the js object
Start loading of html. When html returns, append content to the page and record the id in the array object
This is easy because I use the content for the jQuery get callback directly.
Start loading the js. When the file loads, the js executes and updates the object in the array with a reference to the files return value.
*This is the hard part. jQuery.getScript() automatically executes the script when it completes, so I cant use the return value because its already created. Since I cant use the ajax response I have to have the js file already know the object it will be adding itself to
So, I was hoping there was some js lib already available that does some data-binding between pairs of html and js.
Also, I wasn't sure how to structure the object manager.
Each object in the js files are going to have the same events bound that get called when you move to that section.
Sorry this is kind of a loaded question.
I think my AJAX library might help:
http://depressedpress.com/javascript-extensions/dp_ajax/
One of the most useful features is the ability to define a single "request" (which fires a single handler) with multiple "calls" (individual HTTP calls). You can thus define a single request to contain both the call for the script and a second call for the HTML. When both calls are complete the defined handler will be called and get the results passed to it in the correct order.
The request pool used to process the requests can be instantiated with multiple background objects (allowing multi-threading) so that while the requests get all your data before calling your handlers they're not forced to waste time by single threading (although you can do that as well if you like).
Here's a simple example of how it might be used:
// The handler function
function AddUp(Nums) { alert(Nums[1] + Nums[2] + Nums[3]) };
// Create the pool
myPool = DP_AJAX.createPool();
// Create the request
myRequest = DP_AJAX.createRequest(AddUp);
// Add the calls to the request
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [5,10]);
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [4,6]);
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [7,13]);
// Add the request to the pool
myPool.addRequest(myRequest);
"myRequest" defines the handler. The addCall() methods add multiple calls. Once all three are results will be sent to the handler as an array of three responses (in the same order that the calls were added).
You could calls your files in pairs or even call them all bundled into a single request - up to you.
Hope this helps!
My "application" is a single web page (myPage.html) displaying items on a Google Map. So it is not a scenario where the user navigates among several pages.
Therefor I am using events to track the interactions. However, I find it strange to believe what I see in the statistics.
I have checked the events for syntax and it seems to be OK.
_gaq.push(['_trackEvent', 'MyApp', 'ChangeTav', newTab]);
_gaq.push(['_trackEvent', 'MyApp', 'Load', 'itemType', loadTime]);
....
What I wonder about, I do add Arrays to to _gaq object, but when are these actually send? What happens if the user closes the browser? And is there a way for me to "force" sending these values?
The other way around, user stays on the page for some time, values get added to _gaq, but how I do make sure they not getting lost?
-- Update --
With the push I simply add an Array to an Array, or is this wrong? So in this particular moment nothing happens, I did not see any callbacks or overridden methods. Please correct if I am missing something.
-- Update 2 --
http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html
It seems to be different whether I use sync or async. For async (I am using) ...
To push an API call onto the queue, you must convert it from the traditional JavaScript syntax into a command array. Command arrays are simply JavaScript arrays that conform to a certain format. The first element in a command array is the name of the tracker object method you want to call. It must be a string. The rest of the elements are the arguments you want to pass to the tracker object method. These can be any JavaScript value.
When I do init with var _gaq = _gaq || []; it becomes an array. However, I do never see values being removed from the queue (_gaq), so I'd assume they are never send.
--- OK, here we go ---
http://code.google.com/apis/analytics/docs/gaJS/gaJSApi_gaq.html#_gaq.push
This function is named push so that an array can be used in the place of _gaq before Analytics has completely loaded. While Analytics is loading, commands will be pushed/queued onto the array. When Analytics finishes loading, it replaces the array with the _gaq object and executes all the queued commands. Subsequent calls to _gaq.push resolve to this function, which executes commands as they are pushed.
I believe, it's send as soon as you call it. Provided that the _gaq has been initialized. Depends on where you put the GA init code.
The .push method you are referring to is only for arrays. With Objects, you can define it yourself, you are not overriding anything.
Arrays in js can't have custom indexes / labels, if they do, they are objects.
If you run
var a= {"a":"b"};
var b = ['a','b'];
b.push("c");
a.push("c","d");
You will get an error , function a.push is not defined.
a is an object, b is an array. The push function only exists in the prototype of Array.
To observe when exactly the stuff is tracked, you can use the Net panel in Firebug and check for a _utm.gif file being requested.
I'm switching over our site to use Asynchronous Google Analytics, and I'm curious about it's method of pushing events to the _gaq array. Near as I can tell, events are placed into a waiting pattern in _gaq while the ga.js script is being asynchronously downloaded. Do they fire once the script is downloaded, and how are post-document load events tracked?
One example is a user clicking a link 10 seconds after page load - according to documentation the event should be placed into the _gaq. How does the analytics script detect this?
The general part is best described by the Google Analytics Async doc.
To push an API call onto the queue, you must convert it from the traditional JavaScript syntax into a command array. Command arrays are simply JavaScript arrays that conform to a certain format. The first element in a command array is the name of the tracker object method you want to call. It must be a string. The rest of the elements are the arguments you want to pass to the tracker object method. These can be any JavaScript value.
I'll try to explain the juicy details:
_gaq is just a plain JavaScript Array, and all arrays have the push method to add an entry to the end of the array. So before the Analytics script is loaded, all commands will be pushed to the array. At the end of the Analytics script, it replaces the _gaq.push method with a custom method and executes all entries in the _gaq array. The new _gaq.push method will run the tracking method instantly. So when you run the push method 10 seconds after page load, the command should be executed.
It's always difficult to un-obfuscate the google analytics code, but if I were to solve this problem, upon loading the script, I would redefine the push function on the array after processing everything in the backlog queue (_gaq). This way, the redefined push function would simply act as a proxy to the actual function calls on the pageTracker object.
Here's a simple example of redefining the push method on an array object:
var foo = [];
foo.push("one"); // pushes "one" onto the array
foo.push("two"); // pushes "two" onto the array
foo.push = function(a) { alert(a) }; // redefines `push`
foo.push("three"); // alerts "three"
alert(foo); // alerts "one,two"