Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bubbling up an event from within a module for the parent to handle / take some action on. #15

Open
vishnurk opened this issue Mar 3, 2014 · 2 comments

Comments

@vishnurk
Copy link

vishnurk commented Mar 3, 2014

Hi Ryan,

Quick question.Here's my setup again-

//parent page Html
<div data-bind="module: moduleInfo"></div>

//parent view model
this.moduleInfo=ko.observable(
{ data: "",
  disposeMethod: "dispose",
  id: "moduleId",
  initializer: "initialize",
  name: "modulevm",
  template: "moduleTmpl"
}
);

//module's view model - modulevm
//function that handles a button click event
this.ButtonClick=new function(){
//Do some work.
//How do I let parent know of this event?
}

Is there a recommended way of doing this? Any thoughts / suggestions?

@thestephenmarshall
Copy link

Not sure this is the answer you are looking for:

JS

this.ButtonClick = function(data, event) {
  parent = ko.contextFor(event.target).$parent;
  // or get the root
  root = ko.contextFor(event.target).$root;
  alert(root.name);

HTML

<a href="#" data-bind="click: ButtonClick"> Click me</a>

@vishnurk
Copy link
Author

vishnurk commented Mar 7, 2014

Thanks. I think this will work. I am using KO Lite async command to wire up events to the view model. I am trying to figure out how I can make the recommendation work with async commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants