如何將此函數移植到使用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