Javascript self invoking function not working in angular ionic - javascript

The code inside the controller is:
(function(){
$scope.show();
profile.friend_requests_to_me_service(loggedInUser.id).then(function(data){
console.log(data);
$scope.friend_requests_to_me = data.data.friend_request_to_me;
$scope.friends = data.data.people_you_may_know;
$scope.hide();
});
})();
As you can see this function is calling the service profile and a method of that service.The problem is that this invoking function is not working.The error is given:
TypeError: (intermediate value)(...) is not a function.In plunker i have checked a trivial self invoking function.Here is the link:http://jsfiddle.net/Lvc0u55v/4582/.
This trivial self invoking function is working.But I cant understand why my actual app self invoking function is not working.Thank you for your time.
The present working version(non self invoking) is:
$scope.friendship_requests_to_me_function = function(){
$scope.show();
profile.friend_requests_to_me_service(loggedInUser.id).then(function(data){
console.log(data);
$scope.friend_requests_to_me = data.data.friend_request_to_me;
$scope.friends = data.data.people_you_may_know;
$scope.hide();
});
}
$scope.friendship_requests_to_me_function();

Most probably one of your js statement is missing a ';' at the end.
Try add a ';' before your self invoking function.
Read more here
We end up passing the second function as an argument to the first function and then trying to call the result of the first function call as a function. The second function will fail with a "... is not a function" error at runtime.

Related

Jasmine Unit Testing: How can I spy on Function.prototype.call() in a function parameter that is passed to my method under test?

I want to use jasmine to test a function that takes in a Function parameter
var functionUnderTest = function(functionParameter) {
functionParameter.call();
}
functionParameter above is a Javascript Function and the call method is the method defined on the Function.prototype. How can I spy on the call property to make sure it's called?
The following doesn't seem to be working
it("should call the functionParameter passed to it", function() {
var mockFunction = function() {};
spyOn(mockFunction, 'call');
functionUnderTest(mockFunction);
expect(mockFunction.call).toHaveBeenCalled();
}
as it results in an:
Error: Expected spy call to have been called.
Presumably because the jasmine spy has it's own call() function.
I also tried this, but it isn't working either:
it("should call the functionParameter passed to it", function() {
var mockFunction = function() {};
spyOn(mockFunction.prototype, 'call');
functionUnderTest(mockFunction);
expect(mockFunction.prototype.call).toHaveBeenCalled();
}
this time, the error is:
TypeError: Cannot read property 'slice' of undefined
with a callstack pointing to:
Expectation.toHaveBeenCalled
Is what I'm trying to do possible? I've been at this for hours and it seems like I'm missing something fundamental in my understanding of jasmine.
As suggested by Bergi, the following works:
it("should call the functionParameter passed to it", function() {
var mockFunction = jasmine.createSpy('mockFunction')
functionUnderTest(mockFunction);
expect(mockFunction).toHaveBeenCalled();
}

Sourcing Global Function from an Anonymous Function that Returns in Adobe DTM

The below code snippets are not the actual code, they are only there to explain my issue. So please don't concentrate on the actual functionality. I'm working with Adobe DTM. I have no idea how to get an anonymous function that returns a value (as a Data Element to source a global function? If I have a normal anonymous function within my data Element, everything works fine. If the anonymous function returns, then it doesn't work? Is there any way to get this to work? Example:
//global function
function _myGlobalFunct(str){
return (str);
}
the following code of an anonymous function within the Data Element calls global function and it works as expected:
// working anonymous function
(function () {
window._myGlobalFunct("value1");
})()
but the following return anonymous function, within the Data Element, doesn't call my function but doesn't throw any errors? :
// Not Working Properly but doesn't throw any errors?
return (function() {
var rvalue = document.title || "No Title";
window._myGlobalFunct(rvalue);
return rvalue;
})();
I do know the function is executing but not getting any errors in Chrome?
DTM's data elements execute the code provided within a function (that may not be clear to the other users here), so there will be a return outside of a function in the code you input/show here. You're not returning the value from your function (or if you're trying to update rvalue within the function and rvalue isn't in the right scope (window vs. local)). In any case, is there a reason you're using the anonymous function anyways? Below should work:
var rvalue = document.title || "No Title";
return window._myGlobalFunct(rvalue);
If you still want the anonymous function, make sure to grab the return value from your function:
return (function() {
var rvalue = document.title || "No Title";
return window._myGlobalFunct(rvalue);
})();
I don't think you can return a self-invoking function and then return again in the function.
Since I can't comment yet I will explain it here in more detail, why it is indeed a duplicate. The first answer by Niet the Dark Absol in the link I mentioned above(for reference: Syntax error: Illegal return statement in JavaScript), clearly says the following:
return only makes sense inside a function. There is no function in
your code.
To apply this to your case:
return (function() {
Is your first line, if you would encapsulate everything in another function and call that one everything will be working fine, e.g.:
function myFunction(){
return (function() {
var rvalue = document.title || "No Title";
window._myGlobalFunct(rvalue);
return rvalue;
})();
}
And then you can call myFunction() to get your return value. I hope this helps you out.

How to call an instance method inside an anonymous function?

I just to wanted to call a function inside a self-executing anonymous function. Whenever I try to call a function, I am getting an error saying "Uncaught TypeError: createGesture() is not a function"
loadGesturesFromDatabase: function() {
var firebaseRef = new Firebase("https://test.firebaseio.com/");
this.loadFromFireBase(firebaseRef);
firebaseRef.on('value', function(dataSnapshot) {
dataSnapshot.val();
console.log(dataSnapshot.val()); // console showing the data //which fetched from firebase
//Only having issue when executing following line of code
this.loadJson(JSON.stringify(dataSnapshot.val()));
});
}
loadJson: function(json) {
}
The provided code is not much to go by, but the error is probably caused by the fact that you refer to this inside an anonymous function.
this.loadJson(JSON.stringify(dataSnapshot.val()));
Try storing the this out of the anonymous function, and then using it in the anonymous function, like so
loadGesturesFromDatabase: function () {
var firebaseRef = new Firebase("https://test.firebaseio.com/");
//We keep a reference to the correct this
var _this = this;
this.loadFromFireBase(firebaseRef);
firebaseRef.on('value', function(dataSnapshot) {
dataSnapshot.val();
console.log(dataSnapshot.val()); // console showing the data //which fetched from firebase
//You are now referencing the correct this
_this.loadJson(JSON.stringify(dataSnapshot.val()));
});
}
As a note, there are no self invoking functions in the code you provided. A self invoking function looks like this
(function() {
//code
})()
Mitch identified the problem: since you have a callback function, the meaning of this changes. You can explicitly set the value of this by using bind():
firebaseRef.on('value', function(dataSnapshot) {
this.loadJson(JSON.stringify(dataSnapshot.val()));
}.bind(this));
See: Use of the JavaScript 'bind' method

sports.util.Utils.openNewWindow is not a function

I have showNewWindow, showNewWindowSuccess functions in one js file
and I have openNewWindow function in another js file,
I am trying tto remove the showNewWindowSuccess function from my
success function
but I am getting the following error, Uncaught TypeError:
sports.util.Utils.openNewWindow is not a function
can you guys tell me how to fix it
providing my code below
showNewWindow: function(menu) {
var me = this,
newWindowId = sports.util.Utils.randomString(12);
To declare a function in javascript you need this sintax:
function openNewWindow(param1, param2, param3){
//Do something here...
};

Calling function defined on $scope from another function defined on $scope in AngularJS

I have a controller with many functions defined on $scope.
Ex:
$scope.doSomething = function() {
}
$scope.doSomethingElse = function(data) {
}
From my doSomething function, I want to call my doSomethingElse function. Right now, I am calling it in the success function of my HTTP request (yes, it is succeeding).
Once the success function triggers, I'm calling the other function like so:
angular.scope().doSomethingElse(data); The data variable is passed through as a parameter in the success function.
After running, I'm receiving this error: TypeError: undefined is not a function
Is there another way to do this?
You can just call $scope.doSomethingElse(data); since you've already defined it as a function. As long as it is defined before you call it, this will work.

Categories