Support default Value?
flixyudh opened this issue · comments
Hi, does Vasern support default value?
i need to handle if user is currently install or not,
my usecase something like:
- IF user is first install, show introduction screen, else skip introduce screen
Hey @zxccvvv, Vasern doesn't support default value.
Can you explain the use case a bit more?
Besides, can you specify a default value in your code instead of using default value from Vasern?
Or maybe check if the value is undefined
?
hi @hieunc229
actually i've created my model something like this:
DB.js
import Vasern from 'vasern';
import { CheckInstallState } from './Model';
modelSchema = [
CheckInstallState
];
export default new Vasern({ schemas: modelSchema});
Model.js
export class CheckInstallState {
name = 'CheckInstallState'
props = {
firstInstall:'boolean', // here i want to set default true
}
}
i want to set default value of my model to true, so everytime an app is opened, i want to check if firstInstall
is true or not,
so when i see firstInstall
is true, i want to show On-Boarding
screen and set firstInstall
to false, meanwhile when user open the app more than one, the firstInstall
value should be false forever
Hey @zxccvvv,
I think it wouldn't be much different when having a default value (since it need to wait until the data is loaded).
Without need of the default value, I'd do as following:
// Model.js
export class CheckInstallState {
name = 'CheckInstallState'
props = {
onboardingVisible: 'boolean', // or some better naming
}
isInstalled() {
return !!this.length();
// Maybe, if length === 0, insert a record to mark as onboarded
}
}
Then whenever the app startup, I'd check it as
// import db from "./DB"
...
componentDidMount() {
db.onLoaded(() => {
let firstInstall = db.CheckInstallState.isInstalled();
// if firstInstall === true => show screen
// else => ignore
});
}
...
Is it not possible on your case?
Sorry that I'm caught up with other projects. At the moment, I'll add more feature only necessary
It's sound good to implement your code above,
ill try and closed the issue ATM, thank you @hieunc229
hi @hieunc229 ,
I don't know why vasern doesn't inserting data:
...
componentDidMount(){
//Check if App is first insalled
DB.CheckInstallState.onChange(e=>{
console.log('e', e)
})
DB.onLoaded(()=>{
const isFirstInstall = DB.CheckInstallState.isFirstInstalled()
console.log('isFirstInstall', isFirstInstall)
if(isFirstInstall){
DB.CheckInstallState.insert({showOnBoarding:false})
}
return this.setState({isFirstInstalled:isFirstInstall})
})
}
...
export class CheckInstallState {
name = 'CheckInstallState'
props = {
showOnBoarding:'boolean',
}
isFirstInstalled() {
console.log('data', this.data())
return this.length()===0
}
}
in ComponentDidMount()
console.log('isFirstInstall', isFirstInstall)
always outputing true when i open the app (Swipe App from recent Apps and reopen the app)
am i doing something wrong in didMount
?
Alrighty!
I figured out it was a bug. Because when inserting an item, it will check each property if it's valid.
Somehow, when checking showOnBoarding
, it was invalid (maybe because of the value).
I'll make a fix soon.
For now, if you still want to go ahead, changed showOnBoarding
type to string
. Then when inserting, use a string. For example:
...
DB.CheckInstallState.insert({showOnBoarding: `installed`})
...
Updated: I've pushed a new version 0.3.65 that fixed this error. Thanks again for reporting!
@hieunc229
Thank you for quick reply and fixed
Appreciated
You're welcome @zxccvvv!