我想将我的简约服务器从Coffeescript翻译成Typescript.这是原始代码:
express = require("express")
app=express()
app.use(express.static(__dirname))
app.set("views", __dirname + "/views")
app.set("view engine", "jade")
app.get("/", (req,res)->
res.render "index")
port = process.env.PORT or 5000
server=app.listen(port)
io=require("socket.io").listen(server)
io.sockets.on("connection", (socket)->socket.emit("news", "data"))
Run Code Online (Sandbox Code Playgroud)
这是Typescript版本:
/// <reference path="d.ts/node.d.ts"/>
/// <reference path="d.ts/socket.io.d.ts"/>
/// <reference path="d.ts/express3.d.ts"/>
import express3 = module("express3");
var express = require("express");
var app = express();
app.use(express.static(__dirname));
app.set("views", __dirname+"/views");
app.set("view engine", "jade");
app.get("/", (req, res) => { res.render("index"); });
var port = 5000;
var server=app.listen(port);
import socketio = module("socket.io");
var io=socketio.listen(app);
io.sockets.on("connection", (socket) => { socket.emit("news", "data"); …Run Code Online (Sandbox Code Playgroud) 我想做一些我认为使用"Coffee Script Class"和Angular JS结构的好方法.
<!doctype html>
<html ng-app>
<head>
<meta charset=utf-8>
<meta name=viewport content="width=device-width, initial-scale=1">
<title>Main Test</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/coffee.js"></script>
</head>
<body>
<div ng-controller="MainClass" style="margin-left:10px">
<h2>All todos: {{test()}}</h2>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
请注意,我将DIV ng-controller设置为MainClass并在H2 HTML标记内设置绑定test()方法.
class AngularJSController
constructor: ($scope, $main) ->
$scope.test = MainClass.prototype.test
MainClass.test = MainClass.prototype.test
$main.test = MainClass.prototype.test
test = MainClass.prototype.test
@test = MainClass.prototype.test
console.log @test
class MainClass extends AngularJSController
constructor: …Run Code Online (Sandbox Code Playgroud) 当我用新的Kinetic.Stage替换新的Gallery时,代码正常工作当我使用dirived类时,它不起作用.
为什么从Kinetic.Stage衍生出来的画廊错了?
width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0
height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0
class Gallery extends Kinetic.Stage
constructor: (config) ->
super(config)
window.onload = ->
list_of_photos = jQuery('#_image img')
x_pos = width/4
y_pos = height/4
stage = new Gallery
container: "gallery_container"
width: width
height: height
images_layer = new Kinetic.Layer()
for image in list_of_photos
imageObj = new Image()
imageObj.src = image.src
x_pos = x_pos + 100
y_pos = y_pos + 10
ori = new Kinetic.Image …Run Code Online (Sandbox Code Playgroud) 我正在使用CoffeeScript类和jQuery,并通过ajax加载另一个HTML页面,反过来,它引用另一个javascript,但是在获取ajax加载页面时看到父页面在脚本中加载的类时遇到了问题:
父页面加载一个javascript文件(从CoffeeScript编译):
<script src="/assets/global.js?body=1" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
在CoffeeScript文件中有一个类:
class App
constructor: ->
...
Run Code Online (Sandbox Code Playgroud)
我使用以下方法加载另一个网页:
$.ajax({
url: '/import/show',
success: (data) =>
$('#content').html(data)
})
Run Code Online (Sandbox Code Playgroud)
该页面反过来引用另一个Coffee/JavaScript文件:
<script src="/assets/import.show.js?body=1" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
当这个加载的javascript文件包含:
alert('test')
Run Code Online (Sandbox Code Playgroud)
警报按预期提出.这表明加载代码正常工作.但是,如果子脚本包含:
app = new App()
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Uncaught ReferenceError: App is not defined
Run Code Online (Sandbox Code Playgroud)
如果我将代码放在文档就绪函数中,也会发生这种情况:
$(=> a = new App())
Run Code Online (Sandbox Code Playgroud)
有没有人知道我如何在由父页面加载的脚本中使用由通过ajax加载的子页面加载的脚本中的类?
期望的结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Run Code Online (Sandbox Code Playgroud)
我的循环:
arr = []
n = 0; b = 0
while b < 2
while n < 12
arr.push n
n++
b++
return arr
Run Code Online (Sandbox Code Playgroud)
实际结果 :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Run Code Online (Sandbox Code Playgroud) 这是我的Coffescript Router类:
class App.Router extends Backbone.Router
initialize: ->
console.log 'Router.init'
@on 'all', ->
console.info 'route changed'
routes:
'': 'home'
'test': 'test'
home: ->
console.log 'home routed'
test: ->
console.log 'test routed'
Run Code Online (Sandbox Code Playgroud)
当我重新加载我的localhost页面时,"@ all'all'"回调似乎触发了两次(我的firebug上的double console.info ...)
这是我的firebug输出:
App.init
Session.init
Router.init
home routed
route changed
route changed
Run Code Online (Sandbox Code Playgroud)
正如您所看到的那样,"路径已更改"输出位于我的主路线之后......
最后这是我的引导代码(使用我的App命名空间......),我放置了history.start
App =
init: ->
console.log 'App.init'
@session = new App.Model.Session
@router = new App.Router
# @userPanel = new App.View.UserPanel
Backbone.history.start pushState: true
Model: {}
View: {}
Run Code Online (Sandbox Code Playgroud) 无法让我的Coffee脚本完全编译成:
( function (root) { return 'Hello Coffee'; }(this) );
Run Code Online (Sandbox Code Playgroud)
第一次尝试:
do (root) ->
'Hello Coffee'
Run Code Online (Sandbox Code Playgroud)
...没有生成与上面相同的代码,输出(with --bare):
(function(root) {
return 'Hello Coffee';
})(root);
Run Code Online (Sandbox Code Playgroud) 如何正确关闭socket.io/websocket-client? - 相关,但过时/不起作用
我正在编写一组测试来测试服务器端套接字.在github上获得了jamescarr的代码.Carr的代码基于Liam Kaufman在这里. 传入的文本墙.
这是测试方面.
ioServer = require("socket.io")
ioClient = require("socket.io-client")
chai.should()
chai.use(sinonChai)
expect = chai.expect
server = require('./testUtils/chat-server.coffee')
socketURL = 'http://localhost:5000'
options =
transports: ['websockets']
'force new connection':true
chatUser1 = name:'Tom'
chatUser2 = name:'Sally'
chatUser3 = name:'Dana'
describe "Chat Server", ->
before (done) ->
server.start 5000, done
after (done) ->
server.stop done
it "Should broadcast new user to all users", (done) ->
console.log "new user to all users"
client1 = …Run Code Online (Sandbox Code Playgroud) 任何人都能解释一下hasOwnProperty在空对象上调用了什么吗?为什么要用它?
__hasProp = {}.hasOwnProperty
Run Code Online (Sandbox Code Playgroud)
当我开始用coffescript开发时,我发现了这一点.Ty
我一直在玩CoffeeScript,我用js2coffee进行实验.我注意到的一件事是CoffeeScript处理变量初始化的方式.这个CoffeeScript:
string = 'word'
编译到这个JavaScript:
var string;
string = 'word';
Run Code Online (Sandbox Code Playgroud)
var string;宣言的优点是什么?为什么不
var string = 'word';
coffeescript ×10
javascript ×6
class ×3
jquery ×2
angularjs ×1
backbone.js ×1
controller ×1
express ×1
inheritance ×1
kineticjs ×1
mocha.js ×1
node.js ×1
socket.io ×1
testing ×1
typescript ×1
variables ×1