user4671208
user4671208

Reputation: 70

Running a jquery function from JS

Sorry for the noobish question, but nothing works for me today.

I'm creating a Phonegap application and have intergrated PushWoosh API into my app. And on receive push notification I want to run my previous functions again, so the data will be updated.

Pushwoosh has JS function like this:

document.addEventListener('push-notification',
    function(event) {
        var title = event.notification.title;
        var userData = event.notification.userdata;
        var notification = event.notification;

        if (typeof(userData) != "undefined") {
            console.warn('user data: ' + JSON.stringify(userData));
        }

        var object = JSON.parse(notification.u);

        window.runPushFunctions(object.active, object.open); //Runs a jQuery function I have created..

    }
);

Now window.runPushFunctions looks like this:

$(document).ready(function() {
    window.runPushFunctions = function(active, open) {

        if (active != null || active != undefined) {
            $('.hubs-page').removeClass('hubs-active').hide().eq(active).show().addClass('hubs-active');
        }

        if (open == 2) {
            $('html').addClass('hubs-opening');
        }

        //Trying to run functions from jQuery file that will get data from database and so on..
        received();
        sent();
        checkFriends();

    };
});

But I can't for some reason not run received(), sent(), checkFriends().

These functions is set like this in their own files like this:

(function($) {

    'use strict';
    function checkFriends () {
      $.getJSON('url',function(data){
          $.each(data,function(index,value){
             //Do something with value
          });
      });
 }

Im including files in this order:

file.js -> received(); sent();
file.js -> checkFriends();
file.js -> pushnotifications

Any help will be gladly appreciated

Upvotes: 1

Views: 72

Answers (2)

ArtOfCode
ArtOfCode

Reputation: 5712

As the other answer here says, you are scoping your method definitions so they are not accessible anywhere outside the containing method.

(function($) {

This is a method definition. Any variables or functions non-globally declared within it cannot be accessed outside it. Therefore, you need to define the functions somewhere else or make them global for them to be accessible.

If you go for defining them somewhere else, you can simply move the function definitions to the top of the same file, outside of the (function($) {})() scope.

If you go for global definitions instead, you need to change the methods' defining lines slightly: instead of

function foo() { }

you need

window.foo = function() { }

This assigns an anonymously declared function to an object in the window scope, which is globally accessible. You can then call it using

window.foo();

or simply

foo();

since it is in the window scope.

Upvotes: 3

Cooper Buckingham
Cooper Buckingham

Reputation: 2534

I'm not exactly sure I'm understanding your question, but it looks to me like you are defining the function checkFriends inside of a function scope. If you need access to that function definition, you would need to declare it on an object that can be referenced from the global scope. Obviously the easiest way to do that would be to attach it to the window, though there are plenty of reasons not to do that.

window.checkFriends = function(){//code that does stuff};

Upvotes: 1

Related Questions