how to dynamically add observer methods to an Ember.js object

Posted by Rick Moss on Stack Overflow See other posts from Stack Overflow or by Rick Moss
Published on 2012-11-01T22:42:30Z Indexed on 2012/11/03 17:02 UTC
Read the original article Hit count: 663

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about coffeescript