I have an ion toggle in my application that stores his status in a firebase Database, is there a way to change his status with the data loaded from firebase?
For example, if in the database the status is true, then when I load the view the toggle will be on. I've tried using checked:
<ion-toggle checked="status()" >
where status() returns a boolean.
But due to the async function of firebase, the view loads 1st before the value in status(). Can't find a solution to this problem so far, I'd apreciate any help.
Yes there's a way to do this, but using a function in your attribute is no good. When using a function in the DOM that returns a value you'll make that function executes every time there's a change in the DOM, so since this is a function that fetchs something on Firebase you'll make your user do an request to Firebase every time the DOM is updated.
A good way to do this is storing the Firebase result in a variable. you can do like this when you enter de page:
public myStatus: boolean;
// USING A LIFECYCLE HOOK
ionViewWillLoad(){
firebase.database().ref(`Path/To/Your/Status`).once('value', snapshot => {
this.myStatus = snapshot.val();
});
}
And in your HTML
<ion-toggle [checked]="myStatus">
<!-- You need to bind your checked attribute to myStatus, using checked="{{myStatus}}" also works -->
If you need to always check if your status has changed you can also create and observable on firebase, so if your status change you can change your toggle:
ionViewWillLoad(){
firebase.database().ref(`Path/To/Your/Status`).once('value', snapshot => {
this.myStatus = snapshot.val();
});
firebase.database().ref(`Path/To/Your/Status`).on('child_changed', snapshot => {
this.myStatus = snapshot.val();
});
// YOU'LL NEED BOTH SINCE THE FIRST LOADS THE STATUS FOR THE FIRST TIME AND THE SECCOND CREATES AN OBSERVABLE FOR WHEN THE STATUS CHANGE.
}
Hope this helps.
Related
I am trying to listen for changes inside an Object in firebase realtime database. When I run the code, it prints the values right and once, but when I try to change, add or remove a value inside this Object, the listen function seems to be triggered more than once. When I try to monitor what is happening in firebase, it looks like when I try to add a value, it creates multiple values at first, then removes all of them but one. This seems to be the problem, Does someone know what can be done?
previousReadingsRef.onValue.listen((event) {
previousReadings = event.snapshot.children
.where((element) => element.key.toString().contains("2022"));
for (var x in previousReadings) {
print(x.key);
}
});
I want to call a function when app runs for the first time, which will ask the user to enter firstName , lastName and add a profile picture.
Once the above process is done the function will never again be called during the lifetime of the app.
For something like this you need to check if the user has already entered that data or not. If not then show him the page where he can enter information otherwise take him to HomePage. For this When the user enters the information you need to save it to some persistent storage and check it whenever the app runs. In this way, your function will be called only once until the user deletes the app or clear its memory. You could use the following libraries to store the data.
Hive,
Shared Preference
These libraries save the data in key-value pair and read data faster especially hive.
Use SharePreference
see below code snippet with little advancement in code you can achieve your result.
Future<bool> isFirstTime() async {
var firstTime = SharedPref.pref.getBool('first_time');
if (firstTime != null && !firstTime) {
SharedPref.pref.setBool('first_time', false);
return false;
} else {
SharedPref.pref.setBool('first_time', false);
return true;
}
}
My 'create new User' app can only create one new user. If I want to create an additional user I have to restart the app. What I want to do is, create some button which does this task without restarting the app.
To be more specific, after the initial call of the app I create a new user (include save). The user is stored in the back-end and still visible on the front-end (so I can further edit the user). In case I want to create an additional user, I want to push some button and the view will be restarted/reloaded (and also the Model). Eventually I want the inital state of my app. Then I want to be able to create the next new user (include save) and maybe another one.
What I already tried:
'CrossApplicationNavigation' to my 'create new user' app without any
parameters. It works only the first time, because when I push the
button the second time nothing happens. The URL stays the same (no parameters are changing).
Deleting data of the Model and subsequently calling the oninit()
function. But I get problem with refilling the model.
Is there some function or something else I can try?
As descripted in comment (component.js):
init: function () {
//set model
this.setModel(models.createTableModel(this), "table");
if (this.getComponentData().startupParameters.ID) {
var sID = this.getComponentData().startupParameters.ID[0];
if (sID !== "") {
this.getModel("table").setProperty("/ID", sID);
} else {
this.getModel("table").setProperty("/ID", "");
}
}
}
I think, you're using a local model? Then you'll have to reset your local model. Reset your local model in onInit function and on pressing button "Add new user".
onInit: function(){
this.oLocalModel = this.getModel("myLocalModel");
this._resetModel(this.oLocalModel);
},
_resetModel: function(oModel){
oModel.setData();
},
onPressAddNewUser: function(){
this._resetModel(this.oLocalModel);
}
I am using ag-grid/ag-grid-angular to provide an editable grid of data backed by a database. When a user edits a cell I want to be able to post the update to the backend service and if the request is successful update the grid and if not undo the user's changes and show an error.
I have approached this problem from a couple different angles but have yet to find the solution that meets all my requirements and am also curious about what the best practice would be to implement this kind of functionality.
My first thought was to leverage the cellValueChanged event. With this approach I can see the old and new values and then make a call to my service to update the database. If the request is successful then everything is great and works as expected. However, if the request fails for some reason then I need to be able to undo the user's changes. Since I have access to the old value I can easily do something like event.node.setDataValue(event.column, event.oldValue) to revert the user's changes. However, since I am updating the grid again this actually triggers the cellValueChanged event a second time. I have no way of knowing that this is the result of undoing the user's changes so I unnecessarily make a call to my service again to update the data even though the original request was never successful in updating the data.
I have also tried using a custom cell editor to get in between when the user is finished editing a cell and when the grid is actually updated. However, it appears that there is no way to integrate an async method in any of these classes to be able to wait for a response from the server to decide whether or not to actually apply the user's changes. E.g.
isCancelBeforeStart(): boolean {
this.service.updateData(event.data).subscribe(() => {
return false;
}, error => {
return true;
});
}
does not work because this method is synchronous and I need to be able to wait for a response from my service before deciding whether to cancel the edit or not.
Is there something I am missing or not taking in to account? Or another way to approach this problem to get my intended functionality? I realize this could be handled much easier with dedicated edit/save buttons but I am ideally looking for an interactive grid that is saving the changes to the backend as the user is making changes and providing feedback in cases where something went wrong.
Any help/feedback is greatly appreciated!
I understand what you are trying to do, and I think that the best approach is going to be to use a "valueSetter" function on each of your editable columns.
With a valueSetter, the grid's value will not be directly updated - you will have to update your bound data to have it reflected in the grid.
When the valueSetter is called by the grid at the end of the edit, you'll probably want to record the original value somehow, update your bound data (so that the grid will reflect the change), and then kick off the back-end save, and return immediately from the valueSetter function.
(It's important to return immediately from the valueSetter function to keep the grid responsive. Since the valueSetter call from the grid is synchronous, if you try to wait for the server response, you're going to lock up the grid while you're waiting.)
Then, if the back-end update succeeds, there's nothing to do, and if it fails, you can update your bound data to reflect the original value.
With this method, you won't have the problem of listening for the cellValueChanged event.
The one issue that you might have to deal with is what to do if the user changes the cell value, and then changes it again before the first back-end save returns.
onCellValueChanged: (event) => {
if (event.oldValue === event.newValue) {
return;
}
try {
// apiUpdate(event.data)
}
catch {
event.node.data[event.colDef.Field] = event.oldValue;
event.node.setDataValue(event.column, event.oldValue);
}
}
By changing the value back on node.data first, when setDataValue() triggers the change event again, oldValue and newValue are actually the same now and the function returns, avoiding the rather slow infinite loop.
I think it's because you change the data behind the scenes directly without agGrid noticing with node.data = , then make a change that agGrid recognises and rerenders the cell by calling setDataValue. Thereby tricking agGrid into behaving.
I would suggest a slightly better approach than StangerString, but to credit him the idea came from his approach. Rather than using a test of the oldValue/newValue and allowing the event to be called twice, you can go around the change detection by doing the following.
event.node.data[event.colDef.field] = event.oldValue;
event.api.refreshCells({ rowNodes: [event.node], columns: [event.column.colId] });
What that does is sets the data directly in the data store used by aggrid, then you tell it to refresh that grid. That will prevent the onCellValueChanged event from having to be called again.
(if you arent using colIds you can use the field or pass the whole column, I think any of them work)
I'm playing with Firebase as an alternative to a local Mongo store, for the time being.
I've followed various tutorials, however they are all for older versions of ES6. I've tried to tweak them to v14 and ES6 but, well, no errors but no data!
Some code:
var Rebase = require('re-base');
var base = Rebase.createClass('https://reactathon.firebaseio.com/days');
...
componentDidMount() {
console.log('ExampleComponent Mounted');
base.bindToState('days', {
context: this,
state: 'days',
asArray: true
});
console.log(this.state.days[0]);
}
The console simply logs undefined. I've tried the base URL with and without /days. I've tried getting the data as an object instead of an array. I have a feeling I'm simply pointing at the wrong thing.
Any thoughts?
Cheers.
bindToState is an asynchronous method so it's going to take some time to set up that listener. You're logging before the listener has been set up. As Jacob mentioned in his comment, move your log to your render method and then once your state is bound to Firebase your component will re render and you should see your data.