Skip to content

"Assertion Failed: You can only pass a path to mut" in template only glimmer component #17202

Closed

Description

@GavinJoyce

The following works with a JS backed component (even without a showSomething property defined in JS) but not with a template only glimmer component:

{{#if showSomething}}
  [Here's your something]

  <button onclick={{action (mut showSomething) false}}>Hide</button>
{{else}}
  <button onclick={{action (mut showSomething) true}}>Show</button>
{{/if}}

If I try the same in a template only glimmer component, I get an Assertion Failed: You can only pass a path to mut exception:

screen shot 2018-11-12 at 11 00 21

Here's a 3.5 app with a reproduction: https://github.com/GavinJoyce/template-only-mut-bug

Activity

  1. bekzod commented on Nov 12, 2018

    @bekzod
    Contributor

    great usecase for let helper :P

  2. rwjblue commented on Nov 12, 2018

    @rwjblue
    Member

    Hmm, I don't fully understand what the goal here is. Using showSomething here (unless it is passed as an argument) is ultimately going to try to do a this.showSomething lookup, but with the template only glimmer component feature flag enabled there is no this at all which is most likely causing the error you are seeing. What do you expect to be backing the mutable showSomething binding?

    As a general rule any template only glimmer component's path expressions should either be a block param in scope or a named argument, anything else (IMHO) should error at compile time.

    @GavinJoyce - In this scenario what would you have expected to happen?

  3. rwjblue commented on Nov 12, 2018

    @rwjblue
    Member

    As a general rule any template only glimmer component's path expressions should either be a block param in scope or a named argument, anything else (IMHO) should error at compile time.

    I'm not 100% sure how easy or hard this would be to implement at the moment, since its unknown at template compilation if a given template is going to be used as a template only component (and frankly its possible that templates are shared so it may not be possible...).

    The no-implicit-this template linting rule would be somewhat helpful here, but only in making it clearer that this is being used when it shouldn't be (and also should make the error message clearer). Docs for that here

  4. GavinJoyce commented on Nov 12, 2018

    @GavinJoyce
    MemberAuthor

    great usecase for let helper :P

    This seems to fail in the same way:

    <h3>Template Only Component</h3>
    
    {{#let false as |showSomething|}}
      {{#if showSomething}}
        [Here's your something]
    
        <button onclick={{action (mut showSomething) false}}>Hide</button>
      {{else}}
        <button onclick={{action (mut showSomething) true}}>Show</button>
      {{/if}}
    {{/let}}
  5. GavinJoyce commented on Nov 12, 2018

    @GavinJoyce
    MemberAuthor

    Hmm, I don't fully understand what the goal here is.

    I'm hoping to be able to be able to implement a toggle on a local property in a template only component without needing to define a JS class.

  6. rwjblue commented on Nov 12, 2018

    @rwjblue
    Member

    I'm hoping to be able to be able to implement a toggle on a local property in a template only component without needing to define a JS class.

    Gotcha!

    on a local property

    What specifically does this mean when there is no backing data structure?

  7. rwjblue commented on Nov 12, 2018

    @rwjblue
    Member

    FWIW, the super bad error message is definitely a bug either way. There is even a TODO in the code explaining what is happening:

    // TODO: Improve this error message. This covers at least two distinct
    // cases:
    //
    // 1. (mut "not a path") – passing a literal, result from a helper
    // invocation, etc
    //
    // 2. (mut receivedValue) – passing a value received from the caller
    // that was originally derived from a literal, result from a helper
    // invocation, etc
    //
    // This message is alright for the first case, but could be quite
    // confusing for the second case.
    assert('You can only pass a path to mut', rawRef[UPDATE]);

  8. rwjblue commented on Nov 12, 2018

    @rwjblue
    Member

    One possible solution is what I submitted in a PR to your demo repo GavinJoyce/template-only-mut-bug#1:

    {{#let (data-bag) as |data|}}
      {{#if data.showSomething}}
        [Here's your something]
    
        <button onclick={{action (mut data.showSomething) false}}>Hide</button>
      {{else}}
        <button onclick={{action (mut data.showSomething) true}}>Show</button>
      {{/if}}
    {{/let}}

    Where data-bag returns a stable object for stashing state.

    However, we should really think hard about why we want to keep this a template only component. It does have its own local UI state that it is tracking...

  9. GavinJoyce commented on Nov 12, 2018

    @GavinJoyce
    MemberAuthor

    on a local property

    What specifically does this mean when there is no backing data structure?

    My understanding of templates, which now appears to be wrong, was that they have a local context and that the mut helper could be used to mutate properties on that context. I can see why this mightn't be the case with template only components though.

    I'm keen to replace our most frequently rendered components with template only components, mainly for performance reasons. Not being able to mutate local properties with mut and friends would likely be a barrier to removing all JS from some of these components.

    One possible solution is what I submitted in a PR to your demo...

    That's an interesting approach, thanks!. It seems like it gives us a way forward in our app should we feel that the benefits outweigh the possible downsides

  10. rwjblue commented on Nov 12, 2018

    @rwjblue
    Member

    @GavinJoyce - Depending on Ember version, I'd suggest something like sparkles-component as a possible path forward. It effectively removes any instantiation cost that Ember.Component has (it really is a super tiny base class), and was created to align with future @glimmer/component APIs.

  11. GavinJoyce commented on Nov 12, 2018

    @GavinJoyce
    MemberAuthor

    @rwjblue I'll try that, thanks.

    Let's close this issue?

  12. jgwhite commented on Nov 13, 2018

    @jgwhite
    Contributor

    This thread is going to be very useful to a lot of people as template-only components become more prevalent. I had the same misunderstanding and this is the clearest explanation I’ve seen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions