我正在嘗試設置一個服務來執行json請求到遠程服務器。無法訪問文件外的coffeescript功能
我使用此代碼我services.coffee
腳本里面:
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()
初始化方法工作正常。
我希望能夠從我的項目中的任何地方訪問變量http
。
但是,我無法訪問此文件以外的功能。
如何在全球範圍內定義它?
UPDATE
下面是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);
該腳本在哪裏執行?您的coffescript編譯器是否將所有內容都包含在一個大的IEFE中? – Bergi
目前正在執行所有其他操作。不過,我嘗試過之間執行它,但沒有成功。 –
[CoffeeScript&Global Variables]的可能重複(http://stackoverflow.com/questions/4214731/coffeescript-global-variables) – Bergi