0

From a string I'm trying to instantiate a class (my marionette view). I found a way that works but this way has a problem where I can't actually pass a parameter to the instantiated class. It seems when I call typeMapping[viewType] it's actually returning me Show.OneNode() instead of just Show.OneNode

  class Show.TwoNode extends App.ItemView
    template: "templates/two"
  class Show.OneNode extends App.ItemView
    template: "templates/one"

  class Show.Layout extends App.Layout
    onShow: =>
         typeMapping = {
           one: Show.OneNode
           two: Show.TwoNode
         }
         viewType = "one"      
         view = new typeMapping[viewType]
           model: @model

1 Answer 1

2

again, I would have rather made this a comment, but hey that's life. Have you tried wrapping your values from your key/value pairs in quotes to force them as strings?

typeMapping = {
  one: "Show.OneNode",
  two: "Show.TwoNode"
}
Sign up to request clarification or add additional context in comments.

6 Comments

if I do this I get "typeMapping[viewType] is not a function"
manually stick the parenthesis at the end of the call: view = new typeMapping[viewType]() that should let you pass in your param's as well (assuming it works, it's not something I have personally tried before.
unfortunately in both cases I get the same error with or without parenthesis (), if the typeMapping has them as strings.
nevermind!! my problem was completely unrelated, the above implementation actually works, it was the model I was passing that was invalid, sorry.
ok, as a different approach, Show. is static, so... how about taking that out of the typeMapping section, adding it in the view= new Show['typeMapping[viewtype]](); ? any better?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.