在开发中,我在我的Express服务器上使用Coffeescript和Browserify中间件来提供我的客户端JS,如下所示:
app.use browserify mount: '/client.js', entry: './client.coffee', watch: yes
今天我升级了我的依赖项并在browserify v2网站上注意到了这一点:
在browserify中最糟糕的想法之一,用于托管捆绑包的ad-hoc http服务器中间件终于消失了.
咖啡脚本的默认支持消失了.您仍然可以在程序中使用coffee脚本,您只需要编译为js或者自己将源转换挂钩到bundle管道中.
请记住,如果你不同意我期望很多人会做的这些削减,那么通过v2重构,你可以更容易地展示自己对于浏览器应该如何使用底层新库作为起点的看法.
很公平.
唯一的问题是,我已经阅读了新API的文档,而且我实际上如何实现自己的中间件有点不知所措.事实上,我甚至不能得到一个基本的单机例如使用工作browserify.add()和browserify.bundle(),更不用说作为快递中间件.
我可以继续使用v1,但由于这个项目仍处于开发阶段,我想让我的依赖项保持最新.任何建议都非常感谢.
我到目前为止:
browserify = require 'browserify'
coffee = require 'coffee-script'
through = require 'through'
app.get '/client.js', (req, res) ->
b = browserify()
b.add './client.coffee'
b.transform (file) ->
write = (buf) ->
data += buf
end = ->
@queue coffee.compile(data)
@queue null
data = ''
return through(write, end)
b.bundle {}, (err, src) ->
res.send src
Run Code Online (Sandbox Code Playgroud)
这是有效的,除了我曾经说过require './module'我现在必须这样做 …
我正在玩CoffeeScript,我遇到了一个非常令人沮丧的错误.我有一个集合(我从ajax调用填充 - 不是fetch()- 因为我需要调整返回的JSON)
这是集合
$ ->
class AppName.Members extends Backbone.Collection
model: AppName.Member
Run Code Online (Sandbox Code Playgroud)
和模型
$ ->
class AppName.Member extends Backbone.Model
Run Code Online (Sandbox Code Playgroud)
很简单吧?最终我将构建这些以获取非填充数据,但现在就是这样.而现在我的观点
$ ->
class AppName.TitleView extends Backbone.View
template: JST['templates/title']
initialize: ->
@bind(@collection, 'add', @addOne);
@bind(@collection, 'reset', @addAll);
@bind(@collection, 'all', @render);
$.ajax
dataType: 'json'
url: '/housemembers'
success: (data, textStatus, jqXHR) ->
for member in data.results[0].members
model =
state : member.state
first_name : member.first_name
middle_name : member.middle_name
last_name : member.last_name
next_election : member.next_election
party : member.party
AppName.Members.add …Run Code Online (Sandbox Code Playgroud) 好的,所以我在使用Coffeescript和Backbone进行事件绑定时遇到了一些困难.我有一种感觉,它与我如何初始化一切有关; 我觉得事件授权甚至没有被运行.
这是我的观看代码:
$ ->
class AppName.TitleView extends Backbone.View
template: JST['templates/title']
collection: new AppName.Members
events:
"keypress #search" : "search",
initialize: =>
$('#search').keypress(@search) #doing it manually as a hack
search: ->
console.log('search handler call')
render: =>
$('#app').html(@template)
@delegateEvents() #this doesn't seem to do anything, nor does @delegateEvents(@events)
@
Run Code Online (Sandbox Code Playgroud)
在编译时,它看起来像这样:
(function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] …Run Code Online (Sandbox Code Playgroud) 向动态添加元素时,如下所示:
row = """
<div class="ipad-row">
<h3>Sample Row</h3>
<div id="wrapper-placeholder">
<div class="scrollView" style="">
<a class="btn btn-primary add-row-item">Add an item</a>
</div>
</div>
</div>
"""
$(".add-row").live "click", ->
$(".ipad .body").append row
false
Run Code Online (Sandbox Code Playgroud)
为什么当我尝试从该行元素中的链接获取此信息时,我得到窗口:
$(".add-row-item").live "click", =>
f = $ this
console.log f // this logs window object, not the link element?
f.parent().append row_item
Run Code Online (Sandbox Code Playgroud)
有人可以解释这种行为以及如何使用$(this)就像你在dom加载时元素确实存在一样吗?
谢谢
我的用例如下:
我决定尝试一些nodejs项目的coffeescript,我想要开始我的一些源文件 #!/usr/bin/env node
Coffeescript处理#以评论开头的行.
我知道你可以在.coffee中嵌入js代码,但事实并非如此
file.coffee
`#!/usr/bin/env node`
foo = 'bar'
Run Code Online (Sandbox Code Playgroud)
编译为:
file.js
(function() {
#!/usr/bin/env node;
var foo;
foo = 'bar';
}).call(this);
Run Code Online (Sandbox Code Playgroud) 使用JavaScript或CoffeeScript,有一种简单的方法可以测试以下内容:
if (locals.messages.text === "somestring") { ... }
Run Code Online (Sandbox Code Playgroud)
现在这需要三件事情:locals需要存在, locals.messages需要存在并且需要等于"somestring".
如果locals.messages不存在,它会抛出一个TypeEror cannot read text of undefined
我知道我可以这样做:
if (!!locals && !!locals.messages && locals.messages.text == "somestring) {...}
Run Code Online (Sandbox Code Playgroud)
但这太长了,无论如何:
locals.messages.text使用javascript或coffeescript
我有一个排序数组,我在服务器给他的时候添加了元素.我遇到的麻烦是确定放置新元素的位置,然后将其放在同一个循环中
在javascript中,这看起来像这样
for(var i = 0; i < array.length; ++i){
if( element_to_add < array[i]){
array.splice(i,0,element_to_add);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是在咖啡脚本中我没有访问计数器,所以我不能告诉它将我的数组拼接在所需的索引上.
如何在CoffeeScript中向已排序的数组中添加元素?
我正在使用Rails 3.2.13.
看完这里的一些帖子后,看起来为了在CoffeeScript中使用erb,你的文件只需要有一个.js.coffee扩展名,而不是.js.coffee.erb(奇怪).
但是,我似乎仍无法让它发挥作用.例如,我在demo.js.coffee文件中有以下内容:
console.log '<%= root_url %>'
Run Code Online (Sandbox Code Playgroud)
经过所有处理后,似乎产生:
(function() {
console.log('<%= root_url %>');
$(function() {});
}).call(this);
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,CoffeeScript已经过处理,但erb却没有.这是怎么回事?
我打算使用早午餐来编译coffescript而不是ubuntu
命令:npm install brunch -b我在安装时遇到了一些问题.
npm http 304 https://registry.npmjs.org/coffee-script
npm ERR! error installing brunch@1.6.7
npm ERR! Error: No compatible version found: coffee-script@'>=1.6.2- <1.7.0-'
npm ERR! Valid install targets:
npm ERR! ["0.7.0","0.7.1","0.7.2","0.9.0","0.9.1","0.9.2","0.9.3","0.9.4","0.9.5","0.9.6","1.0.0","1.0.1","1.1.0","1.1.1","1.1.2","1.1.3","1.2.0","1.3.0","1.3.1","1.3.2","1.3.3","1.4.0","1.5.0","1.6.0","1.6.1"]
npm ERR! at installTargetsError (/usr/share/npm/lib/cache.js:488:10)
npm ERR! at next_ (/usr/share/npm/lib/cache.js:438:17)
npm ERR! at next (/usr/share/npm/lib/cache.js:415:44)
npm ERR! at /usr/share/npm/lib/cache.js:408:5
npm ERR! at saved (/usr/share/npm/lib/utils/npm-registry-client/get.js:147:7)
npm ERR! at Object.oncomplete (/usr/lib/nodejs/graceful-fs.js:230:7)
npm ERR! You may report this log at:
npm ERR! <http://bugs.debian.org/npm>
npm ERR! or use
npm ERR! reportbug --attach /home/administrator/npm-debug.log …Run Code Online (Sandbox Code Playgroud) 我今天开始玩CoffeeScript和AngularJS,并注意到没有很多关于如何使用CoffeeScript正确编写AngularJS的文档或示例.我自己的实验似乎没有奏效.作为一个教学上的例外,有人能指出我为什么这个小提琴不起作用?
http://jsfiddle.net/dralexmv/8km8x/4/
它声称没有定义InventoryModule.虽然我已经在第一行宣布了它.
这是HTML:
<div ng-app='InventoryModule' ng-controller='InventoryController'>
<table>
<tr ng-repeat='item in items'>
<td>{{item.title}}</td>
<td>{{item.price | currency}}</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
这就是CoffeeScript:
inventoryModule = angular.module 'InventoryModule', []
inventoryModule.factory 'Items', ->
items = {}
items.query -> [
{title: 'Table', price: '5'},
{title: 'Chair', price: '10'}
]
items
inventoryModule.controller 'InventoryController', ($scope, Items) ->
$scope.items = Items.query
Run Code Online (Sandbox Code Playgroud) coffeescript ×10
javascript ×4
node.js ×3
backbone.js ×2
angularjs ×1
arrays ×1
browserify ×1
brunch ×1
erb ×1
express ×1
if-statement ×1
jquery ×1
jsfiddle ×1
middleware ×1
operators ×1
ubuntu ×1