CoffeeScript 에서 global 로 variable 선언하기
# coffeescript 에서 global 로 variable 선언하기
root = exports ? this
root.Posts = -> new (Mongo.Collection)('posts') # 아니면 다음과 같이 root.foo 대신에 @foo 를 사용하여도 된다.
# 이렇게 하면 Meteor 에서 package scope 로 접근 가능하다
# At the top level, "this" refers to the global namespace on both the client and server
#root.throwError = (message) ->
# @Errors.insert message: message
@throwError = (message) ->
Errors.insert message: message- 위와 같이 하면
- Posts 라는 variable 은 전역에서 access 할 수 있다.
- JavaScript 에서 전역 변수가 필요할 때 var 선언을 하지 않는 것과 같다.
// Generated by CoffeeScript 1.10.0
(function() {
root;
root = typeof exports !== "undefined" && exports !== null ? exports : this;
root.Posts = function() {
return new Mongo.Collection('posts');
};
}).call(this);
//# sourceMappingURL=posts.js.map - 참조
- http://ahoa.tistory.com/191
- Namespacing and CoffeeScript, use variable from a .coffee file with package scope 에 대한 설명 링크 모음 있음.
- CoffeeScript 에서 nested json, dict syntax 사용시 주의사항
- Source Code
- CoffeeScript
#
# CoffeeScript Source Code
#
# TODO - nested json, dict structure 형식 사용시 주의
# Case 1.
Meteor.subscribe 'posts', { sort: submitted: -1, limit: limit }
# Case 2.
Meteor.subscribe 'posts', { sort: { submitted: -1, limit: limit } }
# Case 3.
Meteor.subscribe 'posts', { sort: {submitted: -1}, limit: limit }
# 위의 Case 1. code 는 Case 2. 와 같은 의미이다.
# CoffeeScript 의 괄호를 생략하는 Syntax 를 자주 사용하는데
# Case 1. 과 같은 code 를 작성하면서 Case 3. 의 결과를 바라는 경우가 있다. # 이는 잘못 예측하고 사용한 것이다.
# ComffeScript 가 Javascript 로 바꾸면서 Javascript Code 인 Case 4. 와 같이
# compile 된다.
# so, json, dict 등을 CoffeeScript 에서 사용할 때,
# 위와 같이 nested 된 structure 인 경우, Case 3. 과 같이 반드시 {} 를 사용하자. - JavaScript
//
// JavaScript Source Code
//
// Case 4 : Case 1. Case 2. 와 동일
Meteor.subscribe('posts', {
sort: {
submitted: -1,
limit: limit
}
});
// Case 5 : Case 3. 과 동일
Meteor.subscribe('posts', {
sort: {
submitted: -1
},
limit: limit
}); - 결론은
- 조금 헷갈리거나 복잡하다 싶으면 (), {}, [] 가릴 것 없이 그냥 사용해라.
- (), {}, [] 를 생략하는 Syntax 를 사용한다고, 누가 칭찬해 줄 것도 아니고,
- 유치원생도 알 수 있을 정도로 쉽게 표현된 code 가 가장 Coding 을 잘 한 것이다.