So I have a table that shows the entries. Users click on a button to open a fragment page to edit the data.
app.datasources.SystemOrders.selectKey(widget.datasource.item._key);
app.showDialog(app.pageFragments.SystemOrders_Edit);
This part works fine.
I have changed my datasource to Manual Save Mode to be able to utilize the "email notification for changes" functions that are used in the Project Tracker sample. So that a user can make changes, hit a Save (Close) Button and an email goes out showing the changes.
The problem is that when the user closes the fragment, the table does not update (they have the same datasource). When I was in automatic save mode, I was able to utilize the following to force the table to reload so it reflected any changes:
var datasource = app.datasources.SystemOrders_HideComplete;
datasource.load();
app.closeDialog();
So I figured I just needed to add the widget.datasource.saveChanges(); option for the Close Button.
widget.datasource.saveChanges();
var datasource = app.datasources.SystemOrders_HideComplete;
datasource.load();
app.closeDialog();
Unfortunately, when I use the above I get the following error and the table seems like it gets stuck trying to reload (the spinner keeps spinning).
"Cannot query records on models with changes."
I'm assuming this is maybe because the datasource hasn't finished saving the new change, before trying to reload the datasouce?
How can I have the Save (Close) Button:
Save the changes
Close the dialog
Refresh the table so it reflects the changes?
Thank you for your help.
You can try to refresh datasource in save callback(assuming that you are actually sharing datasource between page fragment and page):
widget.datasource.saveChanges(function() {
widget.datasource.load();
app.closeDialog();
});
That error will happen if your datasource is trying to refresh while you have unsaved changes.
I know your code block calls a client-side saveChanges before loading the datasource again, but you may need to check to make sure that the datasource is not being reloaded elsewhere at the same time.
If that hasn't fixed it, try passing the values to a server-side script and save the changes after assigning the properties, like this:
Client:
function handleSuccess() {
// ensures that records are saved, now reload
app.models.YourModel.YourDatasource.load();
}
google.script.run.withSuccessHandler(handleSuccess).addItem(field_value1, field_value2, field_value3);
Server:
function addItem(field_value1, field_value2, field_value3) {
// create new items
var newItem = app.models.YourModel.newRecord();
// assign values
newItem.field1 = field_value1;
newItem.field2 = field_value2;
newItem.field3 = field_value3;
// save
app.saveRecords([newItem]);
}
I've found this method to be more reliable than manipulating changes from the client script.
Related
I have a model which is in autosave mode. When the user clicks on a button below code is executed.
I want the status to change and get saved and then it should execute the refresh function as the refresh functionality is dependent on the status value. But with the below code refresh function is getting executed before the new status is getting saved.
widget.datasource.item.status='inside';
refreshPanelWithColor();
What I really want to do is this to have the callback functionality but I can't use the saveChanges as it's only for manual save mode.
widget.datasource.item.status='inside';
widget.datasource.saveChanges(function() {
refreshPanelWithColor();
});
How to achieve callback functionality here without switching to manual save mode?
As how Markus Explains in his comment, this functionality is not available at the moment. You can of course use another solution which is using server scripting and reloading the datasource item. In order to achieve that, your client script should look similar to this:
var recordKey = widget.datasource.item._key;
var status = "inside";
google.script.run.withSuccessHandler(function(){
widget.datasource.item._reload(function(){
refreshPanelWithColor();
});
}).withFailureHandler(function(err){
console.err(err.toString());
}).updateDesiredRecord(recordKey, status);
And of course, you need to implement your server script being called from the client. It should look something like this:
function updateDesiredRecord(recordKey, status){
var record = app.models.MYMODEL.getRecord(recordKey);
record.status = status;
app.saveRecords([record]);
}
I'm not sure what your refreshPanelWithColor() function does but I hope you get an idea of what this solution intends to.
You just have to bind your function to datasource on after save event.
I have a WKInterfaceController with a WKInterfaceTable that lists some events a user has recorded in my app.
A user can tap a row of the table to see more details about that event. To accomplish this, I've overridden contextForSegue(withIdentifier:in:rowIndex:) on the WKInterfaceController that contains the table, so tapping a row modally presents a detail view of that row in a new WKInterfaceController called EventDetailController.
The modal presentation is defined on the Storyboard. I can't use push presentation because the WKInterfaceController with the WKInterfaceTable is a page among multiple instances of WKInterfaceController at the top level of my app.
Here's the main issue:
Within the EventDetailController, there's a Delete button to destroy the record that the table row represents.
When a user taps the Delete button, I present an alert that allows the user to confirm or cancel the delete action.
Once the user confirms the record deletion, I want to dismiss the EventDetailController since it's no longer relevant, because it represents a deleted record.
Here's the IBAction defined on EventDetailController that gets called when the Delete button is tapped:
#IBAction func deleteButtonTapped(_ sender: WKInterfaceButton) {
let deleteAction = WKAlertAction(title: "Delete", style: .destructive) {
// delete the record
// as long as the delete was successful, dismiss the detail view
self.dismiss()
}
let cancelAction = WKAlertAction(title: "Cancel", style: .cancel) {
// do nothing
}
presentAlert(withTitle: "Delete Event",
message: "Are you sure you want to delete this event?",
preferredStyle: .alert,
actions: [deleteAction, cancelAction])
}
The problem is that watchOS doesn't seem to allow this. When testing this code, the EventDetailController does not dismiss. Instead, an error message is logged in the console:
[WKInterfaceController dismissController]:434: calling dismissController from a WKAlertAction's handler is not valid. Called on <Watch_Extension.EventDetailController: 0x7d1cdb90>. Ignoring
I've tried some weird workarounds to try to trick the EventDetailController into dismissing, like firing a notification when the event is deleted and dismissing the EventDetailController from a function that's called from an observer of the notification, but that doesn't work either.
At this point I'm thinking there's some correct way I'm supposed to be able to dismiss a WKInterfaceController, or in other words reverse the contextForSegue(withIdentifier:in:rowIndex:) call, but I don't know what it is.
When I call dismiss() directly in the IBAction, instead of in a WKAlertAction handler, it works fine, but I don't like this implementation since it doesn't allow the user to confirm the action first.
I feel like an idiot, but I figured out the solution.
The answer was in Apple's WKInterfaceController.dismiss() documentation the whole time (emphasis added):
Call this method when you want to dismiss an interface controller that you presented modally. Always call this method from your WatchKit extension’s main thread.
All I had to do differently was call self.dismiss() on the main thread.
Here's my updated code for the delete action, which now works as expected:
let deleteAction = WKAlertAction(title: "Delete", style: .destructive) {
// delete the record
// as long as the delete was successful, dismiss the detail view
DispatchQueue.main.async {
self.dismiss()
}
}
Hopefully this will save someone else some troubleshooting time!
I've read about cancelling fetch requests by using AbortController.abort(). Is there a way to start a request again without aborting it after calling this command?
For example, in this demo from MDN, once Cancel download has been clicked, clicking Download video will trigger the fetch again, but immediately abort it.
Is there a way to allow this request again without aborting it? So, in this case, how could you click Download video to begin the download, click Cancel download to cancel the download, and then click Download video again to start the download again? For example, if the user clicked Cancel download on accident...
You can't.
An AbortController or its signal can not be reused nor reseted. If you need to "reset" it, you have to create a new AbortController instance and use that instead.
I think this is by design. Otherwise it could get messy e.g. if you hand over the controller or signal to some external library and suddenly they could remotely un-abort your internal state.
For example, in this demo from MDN, once Cancel download has been clicked, clicking Download video will trigger the fetch again, but immediately abort it.
They fixed the example. After you click Cancel download you will be able to start a new download and cancel it again, over and over. In order to achieve that the Download button instantiate a new AbortController every time, so you get a fresh signal to abort every time:
downloadBtn.addEventListener('click', fetchVideo);
function fetchVideo() {
controller = new AbortController();
signal = controller.signal;
// ...
So it's ok to instantiate new AbortControllers for each request that you may wish to cancel.
I know this might be kind of late, but I'm leaving this answer anyways in case someone needs it.
I don't know if this is the most optimal approach, but in order to keep doing fetch requests (with 'the same' signal) I had to create a new AbortController instance for each request.
In my case (all code being contained inside a class declaration), I deleted and created a new instance every time, like so:
class Foo Extends Bar {
abort_controller_instance = false;
constructor(params){
super(params);
this.resetOrStartAbortController();
}
resetOrStartAbortController(){
if(this.abort_controller_instance){
delete this.abort_controller_instance;
}
this.abort_controller_instance = new AbortController();
}
abortFetchRequest(){
if(this.abort_controller_instance){
this.abort_controller_instance.abort();
this.resetOrStartAbortController();
}
}
...
}
Probably it's not the most elegant solution, but it works.
Regards!
I have a simple master/details relationship where one order can have multiple revenue allocations. The order has a collection that contains these.
I want to sum a property in my revenue allocation objects and ensure that it adds up to my order total. However, if I databind on the count property of the allocations collection this gets called when you first add an empty object and not when that object has been populated. So an empty allocation is added at the time the "Add allocation" screen is created and the databind function called. That of course means that when the save button on the "Add allocation" screen is clicked, the databind function isn't called again.
Anyone got any ideas? I basically want my databind function to be called when the save button is clicked in the "add screen" and not before.
This is the HTML client - NOT Silverlight
I'm pretty sure that the solution would be to use an OData query to get your aggregate data within the databinding function of the save button - or perhaps a separate button (e.g. "Tally Order Totals"). Exactly how you do that? A bit too hard for me to answer right now, but start with a new button TallyOrderTotals and a new data field for your total. Edit the post_render event for TallyOrderTotals and lookup the allocations in the javascript in which you data bind the value of the new data field.
Somewhere you will need a piece of code that looks something like this:
myapp.activeDataWorkSpace.<datasource>.RevenueAllocations
.filter("OrderID eq " + msls._toODataString(<orderID>, ":String"))
.execute()
.then(function (result) {
// assign the result somewhere
}
I'm not saying that's something you can cut-and-paste - but definitely look at the msls.js documentation and see what you can do with querying your data inside the event context.
One quick point however - if you only need to calculate that total as a verification step, consider doing it in the SaveExecuting() event on the server side. This will allow you to throw an exception back up the tree to your HTML page which the msls.js script should render on the client side.
Hope that helps. :)
I'm developing a web application that displays items from a work queue to a user. When an item is selected I have the app lock that item out so no other user can select it. By hitting the back button in the app it unlocks the item.
I want to be able to unlock the item if the user hits the backspace key. I know what code I need to unlock it. I just need to know how to make the code execute on backspace key stroke.
The code that I need to execute will be server side code.
Thanks in advance.
<script>
document.onkeydown = function (e)
{
if (window.event && window.event.keyCode == 8) {
__doPostBack('__Page', 'MyCustomArgument');
}
}
</script>
If you need to execute code on server, you have to change your question accordingly
EDIT:
You could set a Hiddenfield's value to f.e. "unlockItem" and do a document.forms[0].submit() and checkthe hidden-value on serverside or better:
Use the clientside __doPostBack function generated from ASP.Net to submit page(for example on selectedIndexChanged of a DropDownList). You could even generate it from Codebehind if you want the cleanest way.
I changed the above code, but i think your next question could be how you should know which item was selected, won't it?
Then you have to clarify what items we are talking about.
On serverside you get the passed arguments with:
If Page.IsPostBack Then
Dim eventArg As String = Request("__EVENTARGUMENT")
End If
End If