如何将此函数移植到使用coffeescript类语法?Coffeescript转换函数到类
App.PurchaseOrder = (uid) ->
binder = new App.DataBinder(uid, "purchase-order")
# Abstract all this out
purchase_order =
attributes: {}
# The attribute setter publish changes using the DataBinder PubSub
set: (attr_name, val) ->
@attributes[attr_name] = val
binder.trigger uid + ":change", [
attr_name
val
this
]
return
get: (attr_name) ->
@attributes[attr_name]
_binder: binder
# Subscribe to the PubSub
binder.on uid + ":change", (evt, attr_name, new_val, initiator) ->
purchase_order.set attr_name, new_val if initiator isnt purchase_order
return
purchase_order
沿着这条线的东西,但是这不会工作,因为@属性未在构造函数中的binder.on中定义。
class App.PurchaseOrder
constructor: (@id) ->
@binder = new App.DataBinder(@id, "purchase-order")
@attributes = {}
# Subscribe to the PubSub
@binder.on @id + ":change", (evt, attr_name, new_val, initiator) ->
@attributes.set attr_name, new_val if initiator isnt @attributes
return
# The attribute setter publish changes using the DataBinder PubSub
set: (attr_name, val) ->
@attributes[attr_name] = val
@binder.trigger @id + ":change", [
attr_name
val
this
]
return
get: (attr_name) ->
@attributes[attr_name]
只是好奇 - 什么是这里的目标。这个问题有点超出'你''问题的范围,但我很好奇 - 出于学习目的 - 你在这里做什么。 – 2014-11-09 18:01:56
我有一个rails应用程序,我试图用js类来扩充ruby类而不使用客户端框架。在这种情况下,我设置了一个约定,客户端采购订单模型将自动绑定到具有特定数据属性的html元素。因此,如果我有一个页面,其中包含'
这是一个WIP,但您可以在这里查看源代码:https://github.com/mfpiccolo/happy_place_demo。和演示在这里:http://happy-place-demo.herokuapp.com/purchase_orders。通过在输入字段中输入id,您可以直接编辑表格。 – mpiccolo 2014-11-09 21:05:06