I've got a situation where I'd like to notify a grandparent or $parents[1] of a click event that occurs in a sub viewmodel. So basically I'd like to be able to do this
self.$parents[1].actionTaken
I think this doesn't work because of binding context vs viewModel but I'd like to hear if anyone has an ideas on the correct way to do something like this. Thanks
self.save = function () { //do stuff to self first, then self.$parents[1].actionTaken(); };
$parent. This is the view model object in the parent context, the one immeditely outside the current context. In the root context, this is undefined. Example: <h1 data-bind= "text: name" ></h1>
The $data variable is a built-in variable used to refer to the current object being bound. In the example this is the one of the elements in the viewModel.
KnockoutJS is basically a library written in JavaScript, based on MVVM pattern that helps developers build rich and responsive websites. The model separates the application's Model (stored data), View (UI) and View Model (JavaScript Representation of model).
A ViewModel can be any type of JavaScript variable. In Example 1-3, let's start with a simple JavaScript structure that contains a single property called name .
Or use the bind
trick
data-bind="click: $parent.foo.bind($parent)"
Now when you access your foo function the this
keyword will point to the parent context.
foo: function(child) { this.children.remove(child); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With