how to dynamically add observer methods to an Ember.js object
- by Rick Moss
So i am trying to dynamically add these observer methods to a Ember.js object
holderStandoutCheckedChanged: (->
if @get("controller.parent.isLoaded")
@get("controller").toggleParentStandout(@get("standoutHolderChecked"))
).observes("standoutHolderChecked")
holderPaddingCheckedChanged: (->
if @get("controller.parent.isLoaded")
@get("controller").toggleParentPadding(@get("holderPaddingChecked"))
).observes("holderPaddingChecked")
holderMarginCheckedChanged: (->
if @get("controller.parent.isLoaded")
@get("controller").toggleParentMargin(@get("holderMarginChecked"))
).observes("holderMarginChecked")
I have this code so far but the item.methodToCall function is not getting called
methodsToDefine = [
{checkerName: "standoutHolderChecked", methodToCall: "toggleParentStandout"},
{checkerName: "holderPaddingChecked", methodToCall: "toggleParentPadding"},
{checkerName: "holderMarginChecked", methodToCall: "toggleParentMargin"}
]
add_this = { }
for item in methodsToDefine
add_this["#{item.checkerName}Changed"] = (->
if @get("controller.parent.isLoaded")
@get("controller")[item.methodToCall](@get(item.checkerName))
).observes(item.checkerName)
App.ColumnSetupView.reopen add_this
Can anyone tell me what i am doing wrong ? Is there a better way to do this ? Should i be doing this in a mixin ? If so please