我试图在coffeescript/jquery中实现以下内容
- >当用户输入时,检查textBox是否为空(或者有"写评论..."文本),隐藏"添加评论"按钮.否则显示"添加评论"按钮


但是,我面临的问题是从文本框返回给我的val()始终是一个键击.
例如,如果我在框中键入'sad',则target.val()仅返回'sa'.

在输入时如何在文本框中获取最新值?
我的实现如下
events:
"keydown .comment_area": "commenting"
commenting: (e) ->
target = @$(e.currentTarget)
//target.val() is not returning me the latest character entered in the box
if $.trim(target.val()) == "" or $.trim(target.val()) =="Write a comment..."
//hide the 'add' button
@$('.add_comment').hide()
else
//show the 'add' button
@$('.add_comment').show()
Run Code Online (Sandbox Code Playgroud) 我正在使用带有Backbone视图类的CoffeeScript.当我将该类包含在与html相同的页面中时,一切正常.当我使用单独的文件并导出类时,它会初始化但不会映射事件.
类文件是:
root = exports ? this
class root.AppView extends Backbone.View
el: $("#app")
events:
'click #appBtn1' : 'handleEvent'
'click #appBtn2' : 'handleEvent'
initialize: =>
alert 'init'
handleEvent: =>
alert 'event'
Run Code Online (Sandbox Code Playgroud)
只有初始化函数触发.当代码在单独的类中时,我需要更改以映射事件?
我在我的Rails 3.1应用程序中尝试CoffeeScript.但是,我无法弄清楚如何在没有出现上述错误的情况下在CoffeeScript中打破长行
例如,您将如何/在哪里打破以下代码行
alert x for x in [1,2,3,4,5] when x > 2
Run Code Online (Sandbox Code Playgroud)
如果你想要类似的东西
alert x for
x in [1,2,3,4,5]
when x > 2
Run Code Online (Sandbox Code Playgroud)
在我的vimrc中,我已经设置好了
ts=2, sw=2 and I expand tabs.
Run Code Online (Sandbox Code Playgroud)
然而,我无法得到像上面一行那样简单的东西才能正常工作.
我的Gemfile.lock显示了coffee-script-2.2.0和coffee-script-source 1.1.3
if x < change.pageX # pageX is cross-browser normalized by jQuery
val = Number(elem.text())
return elem.text(o.max) if val + o.step > o.max
return elem.text(o.min) if val + o.step < o.min
elem.text(val + o.step)
else x > change.pageX
# same thing, only - instead of +
Run Code Online (Sandbox Code Playgroud)
(咖啡脚本,但你明白了).我正在寻找一个获取布尔值并将其转换为1(真)或-1(假)的技巧.这样我就能做到val + converted_bool * o.step并保存一个if.
我刚刚开始学习coffeescript,它很棒,但它很棘手......
我尝试将javascript中的代码转换为coffeescript
我失败了很多,在一个链接中我贴了3贴
在翻译的js中,我在lambda中返回map的某处出现了"String is not a function"的错误
我现在已经很久没办法解决一个简单的解析器错误了.我在网上找到了几个代码示例,这些代码看起来很相似 - 我会非常感谢提示!
$ ->
$(".category_modal").click (e) ->
alert "Hi"
e.preventDefault()
$.ajax
url: 'entries/detail'
type: "GET" <-- Error: Parse error on line 28: Unexpected '{'
dataType: 'json'
success: (data) ->
alert "Hi again"
$(".category_modal").html(data)
Run Code Online (Sandbox Code Playgroud) 这个coffeescript ......
"""
#{@display_event_small(event) for event in data.top_events}
"""
Run Code Online (Sandbox Code Playgroud)
在data.top_events中的每个元素之间输出逗号.我需要让它们连接在一起而不用逗号.当然,我可以使用更自定义的循环,但我认为CoffeeScript有更好的方法来改变这种行为.
如果我需要澄清,请告诉我.谢谢.
我很困惑 - 我以为我的模型绑定工作正常,但它只是作为一个伪造的ajax请求的jsFiddle.我已将模型绑定到视图,如果我覆盖.fetch()并伪造响应,则一切正常(我可以更新模型和页面上的视图更新).但是,当我不覆盖.fetch()并使用urlRootparam并等待响应时,我会收到错误.
这是工作的jsFiddle,其中模型在.fetch()使用伪造的响应调用后呈现,而不是更改:
http://jsfiddle.net/franklovecchio/FkNwG/182/
所以如果我在服务器端有一个API调用:
/thing/:id
通过示例响应/thing/1:
{"id":"1","latitude":"lat1","longitude":"lon1"}
Run Code Online (Sandbox Code Playgroud)
我注释掉了.fetch(),我得到了控制台错误:
load js core functions core.js:2
init model timeout app.js:114
initializer callback for history, routes app.js:95
App.Layouts.MyLayout onShow app.js:41
App.Regions.MyRegion onShow app.js:25
App.Models.Thing init app.js:55
App.ItemViews.Thing init app.js:87
Uncaught TypeError: Object #<Object> has no method 'toJSON' backbone.marionette-0.8.1.min.js:9
Parsing App.Models.Thing.fetch() response: {"id":"1","latitude":"lat1","longitude":"lon1"} app.js:62
Thing: {"id":"1","latitude":"lat1","longitude":"lon1"} app.js:66
a Thing has changed, update ItemView! app.js:57
Uncaught TypeError: Cannot call method …Run Code Online (Sandbox Code Playgroud) javascript model-view-controller coffeescript backbone.js marionette
这是我的compile.sh:
#!/bin/bash
coffee -o public/app/ -cwb public/src/
Run Code Online (Sandbox Code Playgroud)
当我通过双击打开脚本时:
File not found: public/src/.coffee
Run Code Online (Sandbox Code Playgroud)
它仅适用于终端:./compile.sh
可能有在Mac OS X上编译CoffeScript的最佳方法吗?
我通过CodeKit工具将Backbone.js与Coffeescript一起使用.
到目前为止,我已将所有内容都包含在一个文件中.但我想开始分离它们.在进入AMD和require.js之前,我想尝试一些简单的事情.
例如,我想遵循建议的内容
Backbone.js:将视图,集合,模型分离到不同的js文件,它们无法识别对方
而且由于我使用的CodeKit有一个"附加"(或导入)其他人引用的JS文件的选项,我认为这样可以很好地将所有内容都放在一个文件中.
所以这就是我想要实现的目标.
并通过CodeKit将所有内容编译到App.js中,将视图文件"导入"到App.coffee中.
在我看来,我有以下内容
$ ->
class View extends Backbone.View
el:"#view"
initialize: =>
console.log app
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我有
$ ->
class App extends Backbone.Router
view : new View
routes:
"" : "home"
search: =>
console.log "hi"
app = new App
Backbone.history.start()
Run Code Online (Sandbox Code Playgroud)
现在在我的CodeKit中,我将"View.coffee"导入"App.coffee",以便将它们编译成单个文件.

当我运行它时,我得到"视图"没有定义.
现在,我在这里尝试了各种组合.例如,我尝试使用"window.View"和"window.App"将它们分配到全局名称空间,但这并不成功.我可以这样做,以便App可以阅读View,但我无法让View阅读App.
设置它的最佳方法是什么?或者我做得对吗?还附加了最终的JS输出.
// Generated by CoffeeScript 1.3.1
(function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in …Run Code Online (Sandbox Code Playgroud) coffeescript ×10
javascript ×5
backbone.js ×3
jquery ×3
arrays ×1
bash ×1
code-golf ×1
loops ×1
macos ×1
marionette ×1
string ×1
textbox ×1