2

I am trying to set up a service to perform json requests to a remote server.

I am using this code inside my services.coffee script :

HttpService = () ->

  initialize: -> 
    __Model.List.destroyAll()
    __Model.Item.destroyAll()

    $$.get 'http://localhost:3000/lists.json', null, ((response) ->
      lists = response.lists
      items = response.items

      $$.each lists, (i, list) ->
        __Model.List.create list

      $$.each items, (i, item) ->
        __Model.Item.create item
    ), 'json'

  createList: (list) ->
    $$.post 'http://localhost:3000/lists.json', list, ((response) ->
      ), 'json'

http = new HttpService
http.initialize()

The initialize methods works fine.

What I would like is to be able to access the variable http from anywhere in my project.

However, I cannot access the function outside this file.

How can I define it globally?

UPDATE

Here is the file generated by CoffeeScript

// Generated by CoffeeScript 1.6.3
(function() {
  var HttpService, http;

  HttpService = function() {
    return {
      initialize: function() {
        __Model.List.destroyAll();
        __Model.Item.destroyAll();
        return $$.get('http://localhost:3000/lists.json', null, (function(response) {
          var items, lists;
          lists = response.lists;
          items = response.items;
          $$.each(lists, function(i, list) {
            return __Model.List.create(list);
          });
          return $$.each(items, function(i, item) {
            return __Model.Item.create(item);
          });
        }), 'json');
      },
      createList: function(list) {
        return $$.post('http://localhost:3000/lists.json', list, (function(response) {}), 'json');
      }
    };
  };

  http = new HttpService;

  http.initialize();

}).call(this);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Justin D.
  • 4,946
  • 5
  • 36
  • 69
  • Where is that script executed? Did your coffescript compiler wrap everything in one big IEFE? – Bergi Jul 17 '13 at 17:48
  • It is currently executed after all the others. However, I tried executing it between, but without success. – Justin D. Jul 17 '13 at 17:49
  • possible duplicate of [CoffeeScript & Global Variables](http://stackoverflow.com/questions/4214731/coffeescript-global-variables) – Bergi Jul 17 '13 at 17:58
  • @Bergi, I didn't know it was a Coffeescript related issue. – Justin D. Jul 17 '13 at 18:01

2 Answers2

4

That is because coffeescript wraps the code in a top-level function safety wrapper.

In a browser, you can make it global by doing:

window.http = http

or tell coffeescript to not do the wrapper by compiling with -b:

coffee -c -b services.coffee

In general, global variables aren't a very good idea and you may want to consider using a module system like require.js to organize and access your code (including code in different files).

go-oleg
  • 19,272
  • 3
  • 43
  • 44
3

This will make the variable global in the context of the browser:

window.http = http
Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
Matthew Graves
  • 3,226
  • 1
  • 17
  • 20