Angular seems to fire on its own - angularjs

I have a $http.put function that updates my database, this happens on a button click. After I update the database once, and stay on the page, with no interaction at all the update function gets called and will update the database with the last information that was inputted.
I've triple checked my code, there is only one occurrence of my update function being called. And the $http.put is inside $scope.update that is an anonymous function that calls the $http.put.
If I update information and then leave the page, and then come back the function does not fire on it's own.
Here is my Github for the project. I will pull out any specific code that you want to see, but I'm not sure what to show because there are 4 files involved. Those files are
(client/scripts/app.js)
(routes/products.js)
(public/views/routes/adminIndex.html)
(public/views/routes/modProduct.html)
Code is Here

Add a console.trace() just before the $http call to see what is calling it.
You can also use the developper console of your browser and add a breakpoint that will stop the code execution and let you inspect everything (stack trace, current context, etc). Chrome is particularly good at that.

Related

How to Make a Synchronous Call an Asynchronous Call in Angular?

I have an app that shows statuses for internal processes. It also has a separate view that allows you to set up new records. To set up a new record, you fill out a form, and upon submit a call is made to my nodejs server that:
inserts the record into a table
kicks off a stored procedure
routes you back to the status page
The issue here, is that the page hangs while this happens, as sometimes the stored procedure takes a minute or two to run. So you wait for a couple minutes, and then are routed back to the status page.
Now, I don't actually care to see any exit code for this stored proc on the front end, as you will see the status of it on the status page. I'm wondering if there's a way for me to kick this process off, but not have the front end care about the return.
I've tried adding in the $location.path() before the $http call to the server, but then the $http call never happens.
Any ideas?
Thanks!
You can wrap the stored procedure call in a promise. The browser will make the call and continue on without waiting for it to complete and you can react appropriately in the resolve or reject functions. You can use angular's $q service:
insertRecord();
$q(function() {
storedProcCall();
});
redirect();

Angularjs refresh only angular.element

i working with angularjs, and my web receive data from websocket each second, and now i using a $scope.$apply for refresh every time I get a fact, the problem is when my server send More information than usual the web page is very slow, and if i comment the $scope.$apply the web run fine.
I have something like this
$scope.ChangeData=function(str){
//process.... take around 1ms
//here i change a variable, with new info
$scope.$apply();
//here the process print 60-100ms
}
then i want to do it,
$scope.ChangeData=function(str){
//process.... take around 1ms
//here i change a variable, with new info
angular.element(document.getElementById(str.ID)).scope().$apply
//I do this, I hope the process will only take a few ms
//beacuse i change only the changed element
}
but i have a error in this part
Cannot read property '$apply' of undefined
please help me..
UPDATE
Example in plnkr
Without fully understanding the context of your application, I can only guess that this is a messaging type application. This assumption is based on the fact that a) you're using a socket server to push updates to existing data on the UI and b) you're attempting to maintain existing UI already present.
I took your plunkr and forked it with how I would approach this problem. There are 2 things to realize:
the ItemsController is dumbed down so that it only consumes a model coming from a service and the simulated push to update to said model
the itemsService facilitates
storing the messages data
handles the push update
Because the ng-repeat is already charged with watching any changes in its target collection, you shouldn't have to do any of the $scope.$apply or angular.element stuff you were doing. Each item in the ng-repeat will update the DOM for you. That's its purpose.
https://plnkr.co/edit/qLeDuP0iaTUE16rY2KTl?p=preview

cakephp where do I call a global function which each page needs

I'm using CakePHP (2.2 I think) and am struggling to know where to put a function. It's a function which is called on each page load. The basic logic is
Page is requested
loads page template
loads included header
to check if a cookie has been set.
If cookie is set, use values in that
if cookie hasn't been set, load a value from the database and save in the cookie
save cookie values in a config value to use across the page
continue loading page
Basically the function is to set the config values. I have tried to create a helper which worked nicely as I called it on the header view - but as soon as I try to access the cookie helper it didn't work.
I moved the code in to the app controller and called it on the beforefilter() function, but it seems to get called several times on the one page (even though this does actually all work ok).
So - where would I place a function to do this to get called before ANY html is drawn to the screen and is called only once?
Many thanks,
Matt.
beforeFilter of your AppController.php is the right place. beforeRender should also work, as #zynder mentioned.
beforeFilter should definitely only be called once per request by Cake. If it's working in beforeFilter, but it's being called more than once, you've probably done something wrong, and you should look into that. Or, maybe you could be mistaken in thinking it's executed more than once per request.
Why do you think beforeFilter is being called more than once?

Apex Commandbutton does not fire actionmethod every time

I have an apex page which relies heavily on rerendering certain parts of the page. The issue is that when I click on a commandbutton, normally it calls its action method, and rerenders a part of the page, however, it only works about half the time. On the other occasions the action method does not fire. (Something still happens, because it creates a debug log)
The confusing thing about it is that it happens seemingly randomly (e.g. It works, I reload the page, then it doesn't work, while nothing changed)
Unfortunately I don't have permission to post the code, but any idea would be appreciated.
It was caused by an actionsupport tag inside of a commandbutton which I think should be fine, but it wasn't. I deleted the actionsupport, edited the button itself to contain the rerender and action attributes, and now it works.

Adobe Flex Caching Array

My program fills an array with data from a facebook page feed but every time i go from one tab to another it wants to reload this data, is there any way i can cache this array so that it will not reload the information unless its changed?
This is exactly why your Views should not contain Service logic. Instead, your View should dispatch an event asking for the service call and your Controller (you do have one, right?) should catch that event and decide whether to act on it or not.
How do you know the data hasn't changed without reloading it?
Maybe what you need is to store the timestamp of the last service call, than measure the amount of time before executing the service call again.
Perhaps with a 5-minute timeout, if the user continuously changes tabs within 5-minutes from the last service call, the array persists previously loaded data.
After 5-minutes, if the user changes back to that tab the service call can fire, load data, than update the timestamp to prevent loading.

Resources