kbrsh / moon

🌙 The minimal & fast library for functional user interfaces
https://moonjs.org
MIT License
6k stars 200 forks source link

Computed properties #237

Closed danielo515 closed 5 years ago

danielo515 commented 6 years ago

Hello.

I'm trying to make computed properties work, but it is being an imposible task. Every time I try to access a computed property I get a different error. If I try to iterate a computed properties I get an error related to length of undefined, if I just try to sow the property I get an weird error about an unexpected token ):

const commandComponent = command => Moon ({
    root: '#cmd'
    , view: readFileSync (__dirname + '/todo.html', 'utf8')
    , name: ''
    , data: { command }
    , computed: {
        keys: {
            get: function ()
            {
                return ['a','b']
            }
        }
    }
});

export { commandComponent };
<div>
    <div class="form-group form-inline">
        <label>Name this command: </label>
        <input class="form-control form-control-sm" type="text" @bind={name}></input>
        <button id="cmd-execute" class="btn btn-primary btn-sm">Execute</button>
    </div>
    {{keys}}
</div>
kbrsh commented 6 years ago

There are a couple of issues with your example.

  1. Components are made with Moon.extend, not Moon.
  2. Computed properties exist, but aren't documented yet and use a different way without the tedious computed object.
  3. In your template, you use {{keys}} but the current syntax uses single curly braces.

I'd recommend reading through the documentation or refactoring further, but I'll keep this open until I fix and document computed properties.

kbrsh commented 6 years ago

With the last few commits, I refactored how data is created in components. Computed properties now work like this:

Moon({
  first: "John",
  last: "Doe",

  get full() {
    return this.first + this.last;
  }
});

It should be releasing soon with Moon v1 beta 3.

danielo515 commented 6 years ago

Hello @kbrsh I read the documentation extensively and I didn't found any reference to computed properties. If you can point me to the correct section I'll be grateful. I also researched old issues and code examples on the test folder and I tried all that I found there without luck. I didn't open the issue at first sight, I promise.

The new syntax seems nice, looking forward for it

Regards

kbrsh commented 5 years ago

Moon v1 beta 3 is supports getters and setters now.