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

The default value should be undefined instead of null #13

Closed
harvey-woo opened this issue Mar 23, 2017 · 5 comments
Closed

The default value should be undefined instead of null #13

harvey-woo opened this issue Mar 23, 2017 · 5 comments

Comments

@harvey-woo
Copy link

Such as title
Another problem: default value giving as undefined will be ignore, but set a function returning undefined, the plugin perform correct.

@foxbenjaminfox
Copy link
Owner

foxbenjaminfox commented Mar 23, 2017

Actually, I believe null is appropriate. Keep in mind that the computed property certainly is defined, it's just doesn't have any data yet.

In as much as there is a semantic difference between undefined and null, I definitely feel that this case falls strongly on the side of null.

Given that you have defined the async computed property, it certainly isn't undefined, even before the data is loaded, and so undefined is simply wrong as a default value.

As to your second point: Remember that setting the default value as undefined is the same thing as simply not setting the default value. It's clear that the correct behavior should be exactly the same in both those cases.

If you want to work around this by setting the default to () => undefined then go ahead, I won't try to stop you. But I think that it's pretty clearly not the right thing to do.

@harvey-woo
Copy link
Author

harvey-woo commented Mar 24, 2017

I listened to my colleagues' opinions and referred to some articles on the web. They thought that the difference between undefined and null was as follows

The undefined value is a primitive value used when a variable has not been assigned a value.
The null value is a primitive value that represents the null, empty, or non-existent reference.

I understand what you mean, but for the computed property, I tend to think that it is a primitive value used when a variable has not been assigned a value, not emply or no-existent value

In fact, computed property will only fill the value after the promise is complete, this is what the meaning of undefined

The fact that I use the following code in my project to write data function

  new Vue({
    data: () => ({
      // means data is unclear
      someData: undefine
    }),
    method: {
      async loadData(){
        try {
          this.someData = (await this.$http.get('/someData')).data
        } catch (e){
          console.error(e);
          // means no data
          this.someData = null
        }
      }
    }
  })

The above is my understanding of their differences

Artical references

@foxbenjaminfox
Copy link
Owner

foxbenjaminfox commented Mar 25, 2017

You're right that that's the difference, but I disagree about which one is
relevant in this case.

You say undefined is used when a variable has not been assigned a value.
But in this case, you did assign it a value, in the asyncComputed block,
it's just that the value in question hasn't arrived yet.

In other words, between when you declare the computed property and between
when the property's value is set, the property's value is empty. This,
null which represents an empty value.

That's my opinion, anyway. But arguing about it is fruitless, and I think I
have a solution that you'll be satisfied with, if you remain insistent that
you want the default value to be undefined.

A feature I've been planning (which I'll probably release soon in v3.1.0),
is to allow the user to configure what the global default value is. This
means that you could do something like this:

import AsyncComputed from 'vue-async-computed'
Vue.use(AsyncComputed, {
  default: undefined
})

Then the default value would be undefined, the way you want it to be for
your project.

@harvey-woo
Copy link
Author

That sound good, and i cant wait to do like this in my project with this feature 😊

@foxbenjaminfox
Copy link
Owner

I have now released version 3.1.0 of vue-async-computed, containing, among other things, this feature. I hope it meets your needs.

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

No branches or pull requests

2 participants