我一定是要失去它.我已经设置了最简单的Backbone应用程序,但似乎无法让路由响应.这是我的路由器(在coffeescript中):
class BackboneSupport.Routers.TicketsRouter extends Backbone.Router
initialize: ->
@tickets = new BackboneSupport.Collections.TicketsCollection()
routes:
"/new" : "newTicket"
".*" : "index"
newTicket: ->
alert 'hi, from the new ticket route'
index: ->
// just to prove a point
$('#tickets').html('tickets go here')
@navigate('/new')
Run Code Online (Sandbox Code Playgroud)
我让整列火车继续行驶:
<div id="tickets"></div>
<script type="text/javascript">
$(function() {
window.router = new BackboneSupport.Routers.TicketsRouter();
Backbone.history.start();
});
</script>
Run Code Online (Sandbox Code Playgroud)
正如您所料,根路径(索引)#tickets使用占位符文本填充并成功导航到/新路由(通过地址栏确认),但是,它不会发出任何警报,这意味着该newTicket方法未被触发.
我在这里错过了什么?
更新:
在下面的rjz中,我将导航方法更新为:
@navigate('/new', {trigger: true})
Run Code Online (Sandbox Code Playgroud)
但奇怪的是,仍然没有警报:/
我在咖啡脚本中写了一些行来初始化jquery-ui日期选择器
#this code is working fine
$(".picktime").datepicker
numberOfMonths: 1
#this give me an Error: Parse error on line 3: Unexpected '{'
$(".picktime").datepicker
numberOfMonths: 1
showButtonPanel: true
Run Code Online (Sandbox Code Playgroud)
谢谢.
可能重复:
对象为空?
update: (id, data) ->
toUpdate = @find(id)
if toUpdate isnt {}
console.log "hi mom"
console.log toUpdate
toUpdate.setProperty(key, value) for own key, value of data
return toUpdate
find:(id) ->
result = record for record in @storage when record.id is id
return result or {}
Run Code Online (Sandbox Code Playgroud)
鉴于以下Mocha测试
describe '#update', ->
it 'should return an updated record from a given id and data when the record exists', ->
boogie = createData()
archive = new Archive("Dog")
dog = archive.create(boogie)
result = archive.update(1, {name:"Chompie", …Run Code Online (Sandbox Code Playgroud) 这个jquery代码适用于jquery格式.
我想将此jquery代码转换为coffeescript代码:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.preview_browse img#thumb').attr('src', e.target.result).width(251).show();
};
reader.readAsDataURL(input.files[0]);
$('.not_found_image_browse').hide();
}
}
Run Code Online (Sandbox Code Playgroud)
我从http://js2coffee.org/获得此代码:
readURL = (input) ->
if input.files and input.files[0]
reader = new FileReader()
reader.onload = (e) ->
$(".preview_browse img#thumb").attr("src", e.target.result).width(251).show()
reader.readAsDataURL input.files[0]
$(".not_found_image_browse").hide()
Run Code Online (Sandbox Code Playgroud)
但是当我在post.js.coffee中的项目rails中输入此代码时,对我来说效果不佳.
我在浏览器控制台中遇到此错误:
readURL is not defined
onchange()onchange (line 2)
event = change
[Break On This Error]
readURL(this);
Run Code Online (Sandbox Code Playgroud)
这是我的输入文件中的html代码:
<input id="post_image" class="file required" type="file" onchange="readURL(this)" name="post[image]">
Run Code Online (Sandbox Code Playgroud) 我是CoffeeScript的新手.我正在尝试使用Express.js框架创建Node.js应用程序.在我的快速应用程序配置中,我有这行代码编译错误:
app.use express.static path + '/public'
Run Code Online (Sandbox Code Playgroud)
它正在编译:
app.use(express["static"](path + '/public'));
Run Code Online (Sandbox Code Playgroud)
当我需要这样的时候:
app.use(express.static(path + '/public'));
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会发生这种情况以及如何解决这个问题?它导致我的公用文件夹无法访问.
我正在使用CoffeeScript 1.3.1
我正在使用rails3.2并使用coffeescript
在我的coffeescript中我有一个for循环和每个,我正在执行API调用以获取结果并将结果附加到相应的dom
喜欢
for ajaxLink in _ajaxRenderLinks
_template = $(ajaxLink).attr('data-link-template')
apiURL = $(ajaxLink).attr('data-link-url')
console.log($(ajaxLink))
Myapp.API.execute(apiURL, 'get', {}, (data) ->
console.log($(ajaxLink))
if data isnt "" or data isnt undefined
console.log($(ajaxLink))
$(ajaxLink).empty()
$(ajaxLink).append($(HandlebarsTemplates[Myapp.TemplateRoutes.path(_template)](data))))
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我有两个链接(一个是链接,另一个是ul链接)ajaxRenderLinks有这些a和ul,它有要呈现的模板和获取结果的url.第一个console.log打印第一个链接a,然后为它运行API调用,然后为它运行APi调用,然后打印第二个链接ul并为其运行API CALl.最后它尝试用第二个链接附加最后一个结果,而不是为每个链接添加附加,因为如果数据不是""或者数据不是未定义的话,它内部的ajaxLink仅打印第二个链接而不打印第一个链接然后打印第二个链接
如何解决这个问题
在Ubuntu 10下,我安装使用: npm install js2coffee
似乎安装顺利
我进去了 npm ls
返回:
/root
----js2coffee@0.1.3
--------coffee-script@1.3.3
--------underscore@1.3.3
Run Code Online (Sandbox Code Playgroud)
我尝试执行js2coffee mywebapp.js但得到js2coffee: command not found
怎么了?还有什么需要让js2coffee正常工作?
我刚刚开始学习Rails,我是否也需要学习CoffeeScript?它变得有用吗?
我想在backbone.navigate调用之后运行一个函数(以呈现flash消息).可能吗?我目前挂钩了all事件并检查eventname是否以"route:"开头.但是在我的消息被添加之前,这触发得太早......所以我想在路由完成后运行
# intercept all router navigate and render flash messages if any
renderFlashMessengesOnNavigate = (evt) =>
if evt.indexOf("route:") is 0
console.log "rendering messages after route ..."
@flashMessenger.render()
window.appRouter.bind "all", renderFlashMessengesOnNavigate
window.authRouter.bind "all", renderFlashMessengesOnNavigate
window.userRouter.bind "all", renderFlashMessengesOnNavigate
Run Code Online (Sandbox Code Playgroud) 我在Coffeescript中有一系列值,这样:
arr = ['key1': 1, 'key2': 2, 'key3': 3]
Run Code Online (Sandbox Code Playgroud)
我想将此数组转换为只是值的数组.基本上,
arr.map (iter) -> iter.value # arr => [1,2,3]
=> []
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种这种排列,但我只是不断回到一个空阵列.有小费吗?