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

Need a mechanism to prevent some class properties being transformed to Vue data #98

Open
ericlogic opened this issue Dec 19, 2023 · 7 comments

Comments

@ericlogic
Copy link

Sometimes we don't need all of the class properties to be transformed to Vue data. Actually, I'm in trouble with three.js. When I define a Scene as a class property(It's a very common practice), It is transformed to Vue data and wrapped by Vue so it can't be rendered by three.js any more.

You might suggest that define it as a global variable. But it is not a "class style" way.

So. I suggest to provide a mechanism(maybe a @Nondata decorator) to prevent some class properties being transformed to Vue data.

@ruojianll
Copy link
Contributor

ruojianll commented Dec 19, 2023

You could use vanilla accessors and WeakMap to map vue instance and JS native data.

const insMap = new WeakMap

class Foo .... {
@vanilla
get rawData(){
  if(!insMap.has(this)){
    insMap.set(this,{})
  }
  return insMap.get(this)
}
}

@ericlogic
Copy link
Author

ericlogic commented Jan 8, 2024

Thanks for your suggestion.

It works, but I don't think it's a elegant way. It's not "class style" and make the code a bit obscure. Is it possible to make @vanilla work on properties, just like this:

class Foo ... {
  @vanilla
  private nondata: number = 0;
}

@ruojianll
Copy link
Contributor

It is possible. It may be implemented in next version, but I can't give you a certian time.

@ruojianll ruojianll reopened this Jan 9, 2024
@ruojianll
Copy link
Contributor

Keep this open until we implement the feature.

@ruojianll
Copy link
Contributor

@ericlogic
Copy link
Author

I'm not using Vue Composition API at all. could you give me an example of how to use it in a class-style component.
Thanks!

@ruojianll
Copy link
Contributor

Just wrap your object with markRaw imported from vue wherever you use it.

import {markRaw} from 'vue'
import {...} from 'vur-facing-decorator'
@Component
class C extends Vue{
    field = markRaw(yourObject)
    onMounted(){
         //or
         this.field = markRaw(yourObject)
    }
}

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