我正在通过本文http://blog.shinetech.com/2011/07/25/cascading-select-boxes-with-backbone-js/与backbone.js进行链接选择,但在扩展类时遇到错误.
所以,我有LocationsView类:
class Blog.Views.LocationsView extends Backbone.View
events:
"change": "changeSelected"
Run Code Online (Sandbox Code Playgroud)
CountriesView类:
class Blog.Views.CountriesView extends Blog.Views.LocationsView
setSelectedId: (countryId) ->
Run Code Online (Sandbox Code Playgroud)
CitiesView类:
class Blog.Views.CitiesView extends Blog.Views.LocationsView
setSelectedId: (cityId) ->
Run Code Online (Sandbox Code Playgroud)
但是当coffeescript代码编译为javascript时,我的双扩展类看起来像:
(function() {
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
cities_view.js:5 Uncaught TypeError: Cannot read property 'prototype' of undefined
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child; …Run Code Online (Sandbox Code Playgroud) Rails何时将Coffeescript资源编译为JavaScript?是按需还是在启动时发生?
我有以下CoffeeScript代码:
class Person
secret = 0
constructor: (@name, @age, @alive) ->
inc: -> secret++
Run Code Online (Sandbox Code Playgroud)
其中编译为以下JavaScript代码:
var Person;
Person = (function() {
var secret;
secret = 0;
function Person(name, age, alive) {
this.name = name;
this.age = age;
this.alive = alive;
}
Person.prototype.inc = function() {
return secret++;
};
return Person;
})();
Run Code Online (Sandbox Code Playgroud)
目前secret在所有实例之间共享Person.有没有办法secret在CoffeeScript中创建一个私有实例变量?
如果我有一个类,我将一些参数传递给:
class Foo
constructor: (parameters) ->
@bar = parameters.bar
@moo = parameters.moo
Run Code Online (Sandbox Code Playgroud)
该类创建如下:
foo = new Foo(bar: 2, moo: 8)
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果传递的变量存在,如果不设置默认值,在构造函数中检测的最优雅方法是什么.我在javascript中这样做的方式是:
this.bar = ( parameters.bar !== undefined ) ? parameters.bar : 10;
Run Code Online (Sandbox Code Playgroud)
其中10是默认值.
谢谢你的帮助 :)
好的答案 - 只是为了总结最好的:
为了检测参数是否存在并定义默认值(如果不存在),在javascript中是:
this.bar = ( parameters.bar !== undefined ) ? parameters.bar : 10;
Run Code Online (Sandbox Code Playgroud)
在coffescript中是:
@bar = parameters.bar ? 10
Run Code Online (Sandbox Code Playgroud)
如此优雅和紧凑!
我怎样才能dealViewItem进入FOR循环的范围?目前,dealViewItem的作用域不在其中,我的所有事件监听器都被添加到最后一个dealViewItem中.
for deal in dealArray
dealViewItem = dealViewFactory.DealDetail(deal)
dealViewItem.addEventListener 'click', ->
dealCart.push(deal.dealId)
dealViewItem.setAddedToCart()
btnTakeDeals.setEnabled = true
dealHolder.add(dealViewItem)
Run Code Online (Sandbox Code Playgroud) 我有一个构建链设置,将文件从coffeescript转换为typescript到javascript.我的问题是:将类型签名添加到coffeescript函数的最简单的侵入方法是什么?
coffeescript通过反引号支持原始javascript.但是,这意味着coffeescript不再理解反引号片段.
Coffeescript拒绝这些:
f = (`a:String`) -> a + 2
f = (a`:String`) -> a + 2
Run Code Online (Sandbox Code Playgroud)
我可以在函数上面写这个:
`var f = (String) => any`
Run Code Online (Sandbox Code Playgroud)
它编译,但不进行类型检查.我想这是因为Coffeescript已经宣布了这个变量.
我能弄清楚如何使其工作的唯一方法需要大量的样板
f = (a) ->
`return (function(a:String){`
a + 2;
`})(a)`
Run Code Online (Sandbox Code Playgroud)
在新的Coffeescript Redux编译器中,反引号似乎无法正常工作:https: //github.com/michaelficarra/CoffeeScriptRedux/issues/71
我很清楚这是一个值得怀疑的努力,它现在只是一种体验.我目前使用contract.coffee,但我正在寻找实际类型.
使用CoffeeScript extends与Backbone.js 之间的根本区别是extend什么?
例如,怎么样
class User extends Backbone.Model
Run Code Online (Sandbox Code Playgroud)
不同于
User = Backbone.Model.extend()
Run Code Online (Sandbox Code Playgroud) 如何在node.js中渲染three.js代码?
我想从blender导出,然后打开导出fs并用它渲染场景.
我有.when('/center', '/center/question')我的角度网络应用程序.
当我输入'/center'我的浏览器时,它将重定向到'/center/question'我期望的但是当我点击<a ui-sref="center" href="#/center"></a>它时,它不会重定向并只保留在网址上'/center'.
我的控制台没有错误,我不知道为什么.
我在这里看到一个类似的问题角度UI-路由器$ urlRouterProvider.当不能正常工作了.我尝试了答案,但它仍然没有为我工作.
这是我的coffeescript代码:
whenConfig = ['$urlRouterProvider', ($urlRouterProvider) ->
# default url config
$urlRouterProvider
.otherwise '/center'
.when '/center', '/center/question'
]
stateConfig = ['$stateProvider', ($stateProvider) ->
# nested url config
capitalize = (s)->
s[0].toUpperCase() + s[1..]
mainConfig = ({
name: name
url: "/#{name}"
templateUrl: "templates/#{name}.html"
controller: "#{capitalize name}Ctrl"
} for name in ['center', 'keywordList', 'keywordConfig', 'log', 'stat'])
centerConfig = ({
name: "center.#{name}"
url: …Run Code Online (Sandbox Code Playgroud) 我有一个阵列Arr1 = [1,1,2,2,3,8,4,6].
如何根据元素位置的奇数/偶数将其拆分为两个数组?
subArr1 = [1,2,3,4]
subArr2 = [1,2,8,6]
Run Code Online (Sandbox Code Playgroud) coffeescript ×10
javascript ×6
backbone.js ×2
angularjs ×1
inheritance ×1
node.js ×1
three.js ×1
typescript ×1