canjs / can-stache

Live binding handlebars templates
https://canjs.com/doc/can-stache.html
MIT License
10 stars 13 forks source link

Observables returned by helpers should be read #585

Open phillipskevin opened 6 years ago

phillipskevin commented 6 years ago

If an observable is on the viewModel, stache calls getValue on it. If it is returned by a helper, stache tries to stringify it.

Here is an example:

import { Component, stache, SimpleObservable } from "can";

stache.addHelper("asyncText", () => {
    const text = new SimpleObservable("initial value");

    setTimeout(() => {
        text.set("new value");
    }, 3000);

    return text;
});

Component.extend({
    tag: "my-app",
    view: `
    <p alt="{{ asyncText() }}">{{ asyncText() }}</p>
    <p alt="{{ asyncVal }}">{{ asyncVal }}</p>
  `,

    ViewModel: {
        asyncVal: {
            default() {
                const text = new SimpleObservable("initial value");

                setTimeout(() => {
                    text.set("new value");
                }, 3000);

                return text;
            }
        }
    }
});

The paragraph using the helper renders: <p alt="[object Object]">[object Object]</p> The paragraph using the viewModel value renders: <p alt="new value">new value</p>

phillipskevin commented 6 years ago

This was reported here: https://gitter.im/canjs/canjs?at=5b8fbf4aac25fd11b5e408ef and then continued here: https://gitter.im/canjs/canjs?at=5b90dfb5f5402f32aae63c4d

justinbmeyer commented 6 years ago

This might need to be fixed in can-frag which I think is what takes the result of a helper and tries to prepare it for being inserted in the page.

justinbmeyer commented 6 years ago

Does:

stache.addHelper("asyncText", () => {
    const text = new SimpleObservable("initial value");

    setTimeout(() => {
        text.set("new value");
    }, 3000);

    return text.value;
});

work?

phillipskevin commented 6 years ago

Returning text.value makes the initial value work, but it is not updated when text.set is called.

phillipskevin commented 6 years ago

Here is a codepen: https://codepen.io/kphillips86/pen/XPeaEo?editors=0010