How can I port this function to using the coffeescript class syntax?
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
Something along the lines of this however this will not work because @attributes is not defined in the binder.on in the constructor.
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]
<td data-id="1" data-class="PurchaseOrder" data-attr="state">new</td>and I dopurchase_order = new App.PurchaseOrder(1)thenpurchase_order.set("state", "pending")the html element will be updated.