aurelia-contrib / aurelia-knockout

Adds support for Knockout binding syntax to make transition from Durandal and Knockout to Aurelia simpler.
MIT License
22 stars 4 forks source link

Incorrect scope when with: and compose: expressions used #32

Closed klinki closed 3 years ago

klinki commented 3 years ago

Hello,

I'm porting old project from durandal to aurelia and I'm using your excellent plugin. I'd like to thank you for creating it, it saved me a lot of time.

Unfortunatelly I discovered a problem with combination of with: and compose: expressions. When these two are combined, $root scope is lost and cannot be used anymore.

Here is small examle how to reproduce that issue:

Component:

class Component {
      textValue = ko.observable("Hello World!");
      complexObject = ko.observable({
        textValue: ko.observable("Hello from inner object")
      });
}

View:

    <div data-bind="with: complexObject">
      <div data-bind="compose: { view: 'views/inner-view.html' }"></div>
    </div>

inner-view.html:

<template>
  <h3>This is inner-view</h3>
  <div knockout>
    <table>
      <tr>
        <td>textValue</td>
        <td data-bind="text: textValue"></td>
      </tr>
      <tr>
        <td>$root.textValue</td>
        <td data-bind="text: $root.textValue"></td>
      </tr>
    </table>
  </div>
</template>

or full example on https://codesandbox.io/s/optimistic-leftpad-qjvgm?file=/index.html

I did some debugging with knockoutJS Context debugger Chrome extension (https://chrome.google.com/webstore/detail/knockoutjs-context-debugg/oddcpmchholgcjgjdnfjmildmlielhof) and I found out it correcly saves the $parent scope when with: expression is used, but with compose: it doesn't.

ckotzbauer commented 3 years ago

Hi @klinki, thanks for opening this issue. I'm the creator of this plugin. I'm very proud, that this plugin can help you. It's a long time ago, that I used Durandal or Knockout by myself, but I think I can find some time in the next days to investigate and maybe find a fix for this.

ckotzbauer commented 3 years ago

The fix is included in 2.4.1 All changes: https://github.com/aurelia-contrib/aurelia-knockout/compare/2.4.0...2.4.1

klinki commented 3 years ago

Wow, that was super quick! Thank you!