我有一个处理数据的类。其他类可以给它它们的值,数据类将填充它们。
它看起来像这样:
class Data
constructor : ->
@products = null
populateProducts : (callback)=>
ajaxCall (data)=>
@products = data
callback()
allProducts : (list)=>
if @products?
list = @products
else
@populateProducts => allProducts list
Run Code Online (Sandbox Code Playgroud)
我面临的问题是我添加的每个方法都必须检查产品是否存在。所以我正在研究使这部分代码可重用的方法。
我尝试的一种方法如下:
productsCheck : (callback)=>
if @products?
callback()
else
@populateProducts => products callback
Run Code Online (Sandbox Code Playgroud)
使用这种方法我可以简化所有产品:
allProducts : (list)=>
@productsCheck => list = @products
Run Code Online (Sandbox Code Playgroud)
最后,我正在寻找能够实现以下功能的技术:“如果产品不存在,则从数据库中填充它们”。我可能在想这可能是一种已知的模式,所以如果是这种情况,有关它的信息也值得赞赏。
我想查看所有 mongoose 方法的调用方法日志,如下所示:
# Load Book
LoadBook = (id, cb) ->
console.log 'loading book...'
Book.findById id, (err, book) ->
if err
console.log err
throw err
console.log 'loaded book: ' + book.title
cb book
Run Code Online (Sandbox Code Playgroud)
我想我可以定义post和pre方法是这样的:
BookSchema.pre 'save', (next) ->
console.log 'loading ' + `model_name(don't know how to get it)` + ' ...'
next()
Run Code Online (Sandbox Code Playgroud)
与其他方法相同,例如findByIdorremove但它很长。并且错误处理仅在我不使用回调时才有效,但我每次都使用它。我的意思是:
Part.on 'error', (err) ->
console.log "Got an error", err
Run Code Online (Sandbox Code Playgroud)
我认为有回调时它不起作用,是吗?也许nodejs中有一些通用的分析器?顺便说一下,我正在使用快递。
如果我有两个 div 元素,用户可以在两个选项之间进行选择。
我想表明用户已单击它,因此当用户单击某个元素时,该类会添加一些样式。
现在我只掌握了基础知识。用户只能选择两个元素之一。因此,如果用户已经单击了一个元素,就会出现样式。如果用户决定单击另一个元素,则先前的单击将被删除,然后在新的单击上添加样式。
$(".ind").on("click", ->
$(this).toggleClass("selected")
);
Run Code Online (Sandbox Code Playgroud)
希望有人能指导一下,谢谢!
假设我有一个这种形式的数组
[
[val1,val2,val3],
[val4,val2,val1]
....
]
Run Code Online (Sandbox Code Playgroud)
更新 - 抱歉,我对我的要求不太清楚。我是说这个
输出应该是一个像这样的对象数组
[ {[val1,val2,val3] : 1}, {[val4,val2,val1] : 1}],
Run Code Online (Sandbox Code Playgroud)
我刚刚意识到,上面的 json 非常愚蠢,我认为创建这样的对象更有意义
{ selectedRowIndices: [rows that have the value], freq: the frequency}
Run Code Online (Sandbox Code Playgroud)
我想到了下划线并使用它的 groupBy 函数,实际上,使用简单的数组成功地做到了这一点,使用
groups = _(values)
.chain()
.groupBy(_.identity)
.map((values, key) ->
freq: values.length
value: key
).sortBy((d) ->
d.value
).value()
Run Code Online (Sandbox Code Playgroud)
但是,我不确定如何使用上述数组进行操作。
让我们说一个对象是由一些函数创建的:
myObject = someFunction();
Run Code Online (Sandbox Code Playgroud)
所以现在,myObject是一个对象.
如何为此对象添加新方法?以下内容在Coffeescript中不起作用:
myObject.newMethod: (something) ->
# do stuff here
Run Code Online (Sandbox Code Playgroud)
我无法编辑对象定义someFunction(),所以我必须在事后添加方法到对象.这里的语法是什么?
我有以下rails配置:
Windows 7
Rails 4.1.1
Ruby 2.0.0p481
Gem: coffee-rails 4.0.1
Gem: coffee-script 2.3.0
Gem: coffee-script-source 1.9.1
Run Code Online (Sandbox Code Playgroud)
我有一个工作正常,但现在没有.我不记得改变什么(git diff没有透露任何东西).我得到的错误是:
TypeError: Object doesn't support this property or method
(in c:/Users/mbratc01/Documents/Rails/manpower/app/assets/javascripts/welcome.js.coffee)
Extracted source (around line #9):
7 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => false %>
8 <%= javascript_include_tag 'jquery-1.11.1', 'data-turbolinks-track' => false %>
9 <%= javascript_include_tag 'application', 'data-turbolinks-track' => false %>
10 <%= csrf_meta_tags %>
11 </head>
Run Code Online (Sandbox Code Playgroud)
我真的没有Coffeescript项目了.内容welcome.js.coffee是:
# Place all the behaviors and hooks related to the …Run Code Online (Sandbox Code Playgroud) 我在React(coffee jsx)中有以下组件定义:
CompUtils = require './../utils/comp-utils'
...
render: ->
console.log CompUtils # prints {} (empty object)
<div>
Then
{CompUtils.getConstructComponent @props.construct, @props.onUpdate, @props.onRemove}
</div>
Run Code Online (Sandbox Code Playgroud)
但这有效:
render: ->
console.log require('./../utils/comp-utils')
<div>
Then
{require('./../utils/comp-utils').getConstructComponent @props.construct, @props.onUpdate, @props.onRemove}
</div>
Run Code Online (Sandbox Code Playgroud)
我对此绝对感到困惑.请注意,CompUtils已成功用于其他组件.
我无法找到解决方案.如何将下面的代码转换为更动态,更简洁的内容?
OneComponent = require ('./OneComponent')
TwoComponent = require ('./TwoComponent')
ThreeComponent = require ('./ThreeComponent')
Example = React.createClass
render: ->
filter = @props.filterState
if filter is 'something'
tableCmpt =
<div>
<OneComponent
tasks={@props.tasks}
/>
</div>
if filter is 'somethingElse'
tableCmpt =
<div>
<TwoComponent
tasks={@props.tasks}
/>
</div>
##... etc
return tableCmpt
Run Code Online (Sandbox Code Playgroud) 我正在制作一个射击游戏,我需要通过将它们绑定到 'tick' 事件来更新子弹的状态,但是当调用 remove 方法将它们从 'tick' 事件中删除时,它不会删除它。创建一个新实例后,它会不断更新,而不是绑定的那个。
方法 'add'/'remove' 用于从 'tick' 事件绑定/解除绑定方法
class window.Stage
stage = undefined
counter = 0
fps = 60
add: (element) =>
element.id = counter++
stage.addChildAt(element.view, element.id)
element.listener = createjs.Ticker.on("tick", element.update)
remove: (element) =>
createjs.Ticker.off("tick", element.listener) # Not removing!
stage.removeChildAt(element.id)
update: () =>
stage.update()
Run Code Online (Sandbox Code Playgroud)
这就是我在 Game 类中调用 remove 方法的方式
run: () =>
if @gun? && !@gun.alive
@stage.remove(@gun)
@gun = undefined
if @player.shooting() && !@gun?
@gun = @player.shoot() # Ticker keeps updating new instance
@stage.add(@gun)
for bunker …Run Code Online (Sandbox Code Playgroud) 假设我有一个字符串str = "a b c d e".str.split(' ')给我一系列元素[a,b,c,d,e].我如何使用正则表达式来获得这个匹配?
例如:str.match(/ some regex /)给出['a','b','c','d','e']
coffeescript ×10
javascript ×5
node.js ×2
reactjs ×2
browserify ×1
createjs ×1
jquery ×1
mongodb ×1
mongoose ×1
regex ×1
toggle ×1
toggleclass ×1