coffeescript

Can I use CoffeeScript instead of JS for node.js?

久未见 提交于 2019-12-17 15:01:35
问题 What are my restrictions if I want to code node.js and use CoffeeScript? Can I do anything I'd be able to do in JS? 回答1: Yes, CoffeeScript simply compiles into pure JS, making it completely compatible with node.js. To run CoffeeScripts on node, you can either: Type coffee -c example.coffee to compile, followed by node example.js to run the compiled JS. Simply type coffee example.coffee 回答2: Not only can you run CoffeeScript files directly in Node with coffee source.coffee you can also require

Variable types in CoffeeScript

核能气质少年 提交于 2019-12-17 13:42:44
问题 I'm not quite sure the uses for the different variables in CoffeeScript class Cow @utters = 1 constructor: (@name) -> mutate:-> alert @utters heads: 1 feet = 9 c = new Cow From my investigation, it seems heads is public and feet is private. My confusion comes in when figuring out name and utters . For name it more or less compiles to this.name = name and for utters it compiles to Cow.utters = 1 . So my questions are. What is the scope of utters and how should it be accessed? What is the scope

Variable types in CoffeeScript

你离开我真会死。 提交于 2019-12-17 13:41:23
问题 I'm not quite sure the uses for the different variables in CoffeeScript class Cow @utters = 1 constructor: (@name) -> mutate:-> alert @utters heads: 1 feet = 9 c = new Cow From my investigation, it seems heads is public and feet is private. My confusion comes in when figuring out name and utters . For name it more or less compiles to this.name = name and for utters it compiles to Cow.utters = 1 . So my questions are. What is the scope of utters and how should it be accessed? What is the scope

Conditional operator in Coffeescript

拥有回忆 提交于 2019-12-17 10:46:55
问题 I really like this: var value = maxValue > minValue ? minValue : maxValue; Is there something equally concise in Coffescript? 回答1: value = if maxValue > minValue then minValue else maxValue 回答2: There is a more concise option in both javascript and coffeescript :) value = Math.min(minValue, maxValue) 回答3: As Răzvan Panda points out, my comment may actually one of the better answers: value = `maxValue > minValue ? minValue : maxValue` 回答4: This is a case where it feels like CoffeeScript has

Rails: access controller instance variable in CoffeeScript or JavaScript asset file

心已入冬 提交于 2019-12-17 08:31:43
问题 In Rails 3.1 it is not possible to access controller instance variables in an asset js.erb or coffee.erb file using syntax such as <%= @foo %>, where @foo is set in the controller. So then the question is what are the best ways for passing controller variables to CoffeeScript or JavaScript assets. This question has kind of been asked in multiple convoluted forms on the forum, but my point in asking it again is to have a place where all recommendations are gathered together, and the code

How to access instance variables in CoffeeScript engine inside a Slim template

匆匆过客 提交于 2019-12-17 08:23:10
问题 I have a Rails controller in which I am setting a instance variable - @user_name = "Some Username" In my .slim template I am using coffee engine to generate javascript and want to print out the user name from client-sie javascript code - coffee: $(document).ready -> name = "#{@user_name}" alert name But this is the javascript that is being generated?? $(document).ready(function() { var name; name = "" + this.my_name; alert(name); } How do I access controller instance variables in my

Add a line of code to ALL functions

[亡魂溺海] 提交于 2019-12-17 07:55:23
问题 So I am working in JS a lot, and I am working a lot with events (try to stay as modular as possible). Current I am calling Event.fire('eventName') at the end of every function. I am looking for a way to have ANY function in my object/class automatically call an Event.fire([function name]) at the end of all functions Example: function MyClass(){ this.on('someFunc', this.funcTwo); this.someFunc(); } MyClass.prototype.on = function( event ){ // the event stuff // } MyClass.prototype.someFunc =

onkeyup and onfocusout is not working in jQuery Validate

岁酱吖の 提交于 2019-12-17 07:51:19
问题 I am using jQuery Validate plugin for the validation purpose. I have to real time validation on the form. For example if a first name is required, then user should get the required message for first time and goes when he start to type in the text box. To achieve this functionality I am using on onkeyup and onfocusout options of the plugin. For this I refer this link who has the same issue. Here is my coffeescript code for form validation defaultPageForm: (self)-> form = $("#PageForm") if form

onkeyup and onfocusout is not working in jQuery Validate

ε祈祈猫儿з 提交于 2019-12-17 07:51:17
问题 I am using jQuery Validate plugin for the validation purpose. I have to real time validation on the form. For example if a first name is required, then user should get the required message for first time and goes when he start to type in the text box. To achieve this functionality I am using on onkeyup and onfocusout options of the plugin. For this I refer this link who has the same issue. Here is my coffeescript code for form validation defaultPageForm: (self)-> form = $("#PageForm") if form

Is there any way to not return something using CoffeeScript?

二次信任 提交于 2019-12-17 04:47:20
问题 It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality? 回答1: You have to explicitly return nothing, or to leave an expression evaluating to undefined at the bottom of your function: fun = -> doSomething() return Or: fun = -> doSomething() undefined This is what the doc recommends, when using comprehensions: Be careful that you're not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value —