0

I have some code like this:

(function() {
    'use strict';

    angular.module('outApp.core')
        .factory('socket', socketDataService);

    function socketDataService() {

        var service = {
            openSocket: openSocket,
            closeSocket: closeSocket,
            getUpdates: getUpdates,
            sendUpdates: sendUpdates,
        };

        return service;

        function openSocket() {
            var contextPath = document.location.href.split('/#')[0];
            var sock = new SockJS(contextPath + '/api/cart');
            var stompClient = Stomp.over(sock);

            stompClient.connect({}, function(frame) {
                console.log('Connected ' + frame);
                stompClient.subscribe("/app/cart", {}, { customerId: "4654654", cartId: "54654" });
            });
        }

There are other named functions as shown in the JS object named service at the beginning but I'm excluding them for brevity.

I am trying to grant access to the functions I mentioned in the service object using a factory constructor that should be injectable and therefore accessible in our entire app.

I have two questions: Is this the correct way to define and return the methods?

I know I can define:

var service.method = function () {
     //do stuff
}
var service.anothermethod = function () {
     // do other stuff
}
return service;

but for clarity, readability and style we have chosen to do it this way. I have seen another developer do it the first way but I'm not clear on what is going on.

Next question: how can I access and thus test these methods within the function? I set a debugger at the end of the factory but I am not able to see the methods in the console.

Thanks!

3
  • There are several js issues in your code. You define a function after returning, and you mix variable declaration and property setting Commented Mar 4, 2015 at 21:48
  • There's nothing that forbids returning before defining a function that way actually. It works fine. Although it's generally not the best idea. Commented Mar 4, 2015 at 21:51
  • Is this a service or factory you are trying to do? There's a difference between the two. Check out here: stackoverflow.com/questions/14324451/… Commented Mar 4, 2015 at 21:54

1 Answer 1

0

Doing this:

function foo() {
    bar();

    function bar () {
        [...]
    }
}

Is basically the same thing as doing this:

function foo() {
    var bar = function () {
        [...]
    };

    bar();
}

A function is just a type of object that you can assign, pass as an argument to others functions, etc. Don't think about is as a method like in Java.

The 2 ways you're mentioning are both fine. although there is no "var" in the 2nd way:

service.method = function () {
    //do stuff
};

You're just setting a property of the object, the same way you would with an int:

service.foo = 42;

Other than that, I'm not sure about what you're asking.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.