coffeescript

How to send result of a function after the loop is finished?

时光怂恿深爱的人放手 提交于 2019-12-25 05:46:08
问题 In a Node/Express server in CoffeeScript, I have the following function : @resolveServers = (url, servers, answer) -> result = [] treatServer(url, server, (treatAnswer) -> result.push(treatAnswer) ) for server in servers answer(result) The treatServer method take some times, and the answer is send before the loop is finish. How can I send the result only when the loop is come at an end ? Thanks for the help. 回答1: It is easy as long as you know the number of servers beforehand: @resolveServers

Coffeescript: how to link div, but override link if child links are clicked

為{幸葍}努か 提交于 2019-12-25 04:58:15
问题 I find it hard to explain, so please request more info if you need it. I have a div like so: <div id="clickable"> <p>Some content</p> <a href="google.com" class="link">Google</a> <a href="yahoo.com">Yahoo</a> </div> I want the div to be clickable, and link to the href of an attribute with class 'link'. This part I have done: $('#clickable').on 'click', (ev) -> window.location = $(this).find('.link').attr('href') return false But, what should happen is if a user clicks a link within the div,

$q.all - take only those that resolved

感情迁移 提交于 2019-12-25 03:24:31
问题 in case like that: getCol = (colId)-> dfrd = $q.defer() if colId == "bacon" dfrd.reject() else dfrd.resolve colId dfrd.promise getCols = (columns)-> $q.all(_.map(columns, (cs)-> getCol(cs))) getCols(['eggs','juice']).then (cols)-> console.log cols # works getCols(['eggs','juice','bacon']).then (cols)-> console.log cols # not even getting here So, in getCols() how can I return only those promises that's been resolved? 回答1: $q.all is meant only to resolve when all of the promises you pass it

How to use inline :confirm option for html helpers with AJAX calls?

丶灬走出姿态 提交于 2019-12-25 02:56:08
问题 I am trying to have an AJAX implementation of record deletion associated with a button. The problem is that ajax:success event doesn't seem to be triggered in such case. I have implemented the suggestion from this post: Rails :confirm modifier callback?) but I am uncertain if it's the preferred way. I was wondering if communal wisdom could help in this case. What's the right approach here? app/views/competitions/show.html.haml: %td= button_to 'Delete', contender, remote: true, method: :delete

NodeJS Express app generation with CoffeeScript and HAML

ε祈祈猫儿з 提交于 2019-12-25 02:26:45
问题 I just started looking into NodeJS and Express and found out about the possibility to generate a new app that uses hogan right from the start: express <appname> -c [stylus, less] --hogan --ejs Is there a way to generate a new app that uses CoffeeScript, HAML instead of Jade and Less/SCSS ? 回答1: I don't think there is a generator, but you can easily make an express app using HAML and coffeescript. package.json: { "name": "haml-coffee-express", "dependencies": { "express": "", "express-partials

Coffee script multidimensional array creation

爱⌒轻易说出口 提交于 2019-12-25 02:21:50
问题 So I'm trying to get a multidimensional array to work in CoffeeScript. I have tried with standard Python list comprehension notation, that makes the inner bracket a string or something. so I can't do list[0][1] to get 1, I instead get list[0][0] = '1,1' and list[0][1] = '' [[i, 1] for i in [1]] Using a class as the storage container, to then grab x and y. Which gives 'undefined undefined', rather then '1 1' for the latter part. class Position constructor:(@x,@y) -> x = [new Position(i,1) for

CoffeeScript : unexpected INDENT error

泄露秘密 提交于 2019-12-25 02:21:25
问题 I am trying out CoffeeScript in my Rails 3.1 app. However, I am not able to figure out how to break long lines in CoffeeScript without getting the above error For example, how/where would you break the following line of code alert x for x in [1,2,3,4,5] when x > 2 if you wanted something like alert x for x in [1,2,3,4,5] when x > 2 In my vimrc, I have set ts=2, sw=2 and I expand tabs. And yet, I cannot get something as simple as the line above to work properly. My Gemfile.lock shows coffee

Easy way to convert a string to a number or null?

随声附和 提交于 2019-12-25 02:03:21
问题 Is there a slick way to convert a string to a number or null if it cannot be represented by a number? I have been using the method below: if _.isNaN( Number(mystring) ) then null else Number(mystring) Which works, but I'm curious if there is something that is shorter? Possible, or is this the most succinct way? 回答1: If you don't care for "0", then you can use +s||null If you want to support "0" , then I don't have better than 1/s?+s:null 回答2: This is a small improvement to the OP's answer,

CoffeeScript and jQuery plugin authoring

那年仲夏 提交于 2019-12-25 01:44:31
问题 I have a habit of making questions unnecessarily long by including off-topic and useless rants. I'll try not to do it this time. I apologize for the lack of a better and more descriptive title. So, here's a CoffeeScript code. (($, window, document) -> # Conventionally 'private' variables _name = 'aPlugin' _defaults = property: 'value' _debug = (args) -> console.log args return # Plugin constructor Plugin = (element, options) -> @element = element @options = $.extend true, {}, _defaults,

Stopping setInterval in coffeescript

左心房为你撑大大i 提交于 2019-12-25 00:52:25
问题 How can I stop the setInterval in my script ? Alert working, clearInterval not, and there isn't any error in console. order = start_poll: -> interval = setInterval(@request, 60000) stop_polling: -> clearInterval order.start_poll() alert ('expired') request: -> id = $('#status_id').attr('data-id') $.get "/orders/#{id}", (data) -> console.log("#{data.status}") if data.status is 'expired' order.stop_polling() $ -> order.start_poll() 回答1: setInterval returns a new timer ID every time you call it