0

In DurandalJS I've created an Activator in my ViewModel which will be bound against a Compose Binding in the View. Trivial example:

var ViewModel = function(){
    this.childView = activator.create();
    this.activate = function(whyIsThisUndefined){ console.error('Well?' + whyIsThisUndefined); }
}


<div data-bind="compose: { model : theModel, activationData: [1]}"></div>

What am I doing wrong? Why isn't Durandal passing the activation parameters?

Oliver Kane
  • 888
  • 1
  • 6
  • 23
  • 1
    Check this post - http://pwkad.wordpress.com/2014/02/02/best-parts-about-durandal/ – PW Kad Feb 18 '14 at 21:44
  • Hey, PW, Kad, I actually just read that article. I'm aware of that syntax and found several references to it in my googling, but even using hard-coded values, nothing is being passed to my activate methods. Do you have a working example I could see? What version of Durandal are you using? (2.0.1 for me). – Oliver Kane Feb 18 '14 at 21:47
  • 2.0.1 in that sample. Can you change your activation data to activationData: { data: 'Hey!!!!' } and then console.log(whyIsThisUndefined) and see if there is a data property on that object? – PW Kad Feb 18 '14 at 22:01
  • Regardless of what I place as the value of activationData, nothing gets passed into the activate function. Eg: if I were to call console.log(arguments) there would be none. There is no error. It behaves as if "activationData" isn't recognized, or was mispeled. Similar to how ttext would be bound. There's no matching binding so it's just silently ignored. – Oliver Kane Feb 18 '14 at 22:07
  • 1
    I know Rainer has working examples of almost everything here - http://dfiddle.github.io/dFiddle-2.0/#extras - it's possible you could find one on activation that helps you debug, but my best guess is that your activate function may not be getting passed data because it is a singleton, but again, that is a guess. – PW Kad Feb 18 '14 at 22:12
  • Ooooo...if it's a singleton it won't get those parameters passed? Curious. That gives me something to work with. the "theModel" in the above isn't the constructor, but rather an instance. I'd bet money that makes it behave as a singleton. Even though there's multiple instances. Will check now. – Oliver Kane Feb 18 '14 at 22:13
  • That was it! How I wish I knew that hours ago. So...today I learned that DurandalJS doesn't pass activation params to vm singletons. If you make a Answer, I'll credit you, kind sir. – Oliver Kane Feb 18 '14 at 22:19

1 Answers1

3

When you use Singleton's for your view models, I don't think the parameters are passed to the activate function each subsequent time. Pass in your data as such -

activationData: { data: 'Hey!!!!' } 

And make sure you are creating activate onto the prototype and creating a new instance of the view model (or AMD module) each time.

PW Kad
  • 14,953
  • 7
  • 49
  • 82
  • About adding activate() to the prototype...why is that? I've been attaching that to the newly created instance itself without issue so far. It would be a trivial change, but I'd like to understand the "why" behind doing so. – Oliver Kane Feb 18 '14 at 22:29
  • If you're using a constructor function say `ctor = function(){}`, you'd put `activate` on `ctor.prototype` so that it could be shared across multiple instances. No reason to use it on singleton, though, probably a typo. – RainerAtSpirit Feb 18 '14 at 22:52
  • Read out of context, I meant when you are *not* using singleton and using a constructor function, attach activate onto the prototype. – PW Kad Feb 18 '14 at 22:55