Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set/get data within custom method? (this.set/get not working) #179

Closed
rgeraldporter opened this issue Dec 11, 2016 · 6 comments
Closed

Comments

@rgeraldporter
Copy link

Not sure if this is a bug or I'm doing it wrong, but this.set and this.get within a custom method do not appear to work. Or am I doing this wrong?

e.g., the following doesn't work in REPL

<h1>Hello {{name1}}!</h1>
<h1>Hello {{name2}}!</h1>
<button on:click='swap()'>Swap!</button>

<script>
    export default {
        data () {
            return {
                name1: 'Joe',
              	name2: 'Bob'
            }
        },
        methods: {
            swap: () => {
                let nameA = this.get('name1');
              	let nameB = this.get('name2');
              	this.set({name1: nameB, name2: nameA});
            }
        }
    }
</script>
@jihchi
Copy link

jihchi commented Dec 11, 2016

https://svelte.technology/repl/?gist=15529b32e4e6ffb42e06a77d4b7b7df1

Use method properties in methods.

@nikku
Copy link
Contributor

nikku commented Dec 11, 2016

The validator should probably throw an error here.

@nikku
Copy link
Contributor

nikku commented Dec 11, 2016

Alternative (ES 5):

methods: {
  foo: function(ab, cd) {

  }
}

@Rich-Harris
Copy link
Member

The validator should probably throw an error here.

Yes, this is a good idea. I'd say warning rather than error because there are occasions when you don't need this (e.g. log: msg => console.log(msg), but that would be useful information to provide

@Rich-Harris
Copy link
Member

Actually, we can improve on that – if the arrow function expression doesn't use this or arguments, no problem. If it does, error.

Rich-Harris added a commit that referenced this issue Dec 11, 2016
error if method is an arrow function expression and uses `this` or `arguments`
@rgeraldporter
Copy link
Author

Oh wow, good point. Thanks for the fast turn-around on this.

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

No branches or pull requests

4 participants