The activity.write function should be overridden by each activity that wants to store data, with its own write function. It will be called when the activity goes to the background (pause) or before the activity window closes (stop).
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,6 +16,20 @@ define(["webL10n", | ||
|
|
||
| l10n.start(); | ||
|
|
||
| function onPause() { | ||
| activity.write(function () {}); | ||
| } | ||
|
|
||
| function onStop() { | ||
| function onDataStored(error, result) { | ||
| activity.close(function () {}); | ||
| } | ||
| activity.write(onDataStored); | ||
| } | ||
|
|
||
| bus.onNotification("activity.pause", onPause); | ||
| bus.onNotification("activity.stop", onStop); | ||
|
|
||
| datastoreObject = new datastore.DatastoreObject(); | ||
|
|
||
| var activityButton = document.getElementById("activity-button"); | ||
| @@ -33,9 +47,7 @@ define(["webL10n", | ||
|
|
||
| // Make the activity stop with the stop button. | ||
| var stopButton = document.getElementById("stop-button"); | ||
| stopButton.addEventListener('click', function (e) { | ||
| activity.close(); | ||
| }); | ||
| stopButton.addEventListener('click', onStop); | ||
|
|
||
| shortcut.add("Ctrl", "Q", this.close); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
|
||
| @@ -78,6 +90,14 @@ define(["webL10n", | ||
| bus.sendMessage("activity.get_xo_color", [], onResponseReceived); | ||
| }; | ||
|
|
||
| // Activities should override this function in order to store | ||
| // data. | ||
| activity.write = function (callback) { | ||
This comment has been minimized.
Sorry, something went wrong.
dnarvaez
|
||
| setTimeout(function () { | ||
| callback(null); | ||
| }, 0); | ||
| }; | ||
|
|
||
| activity.close = function (callback) { | ||
| function onResponseReceived(error, result) { | ||
| if (error === null) { | ||
3 comments
on commit 3c2420f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay :( I somehow missed the notification for this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really a javascript master but is "overriding" functions like this considered good practice? In python I would only do it for unit tests and such, never in the real code. I wonder if DOM custom events are better for this kind of thing.
Yeah, after playing with it in some of my activities I don't like the override. A better approach is to pass the function in activity.setup(), or have another method in activity to subscribe the function to call when write is needed. What about this?
function write() {
var jsonData = JSON.stringify(gearSketch.board);
return jsonData;
}
function read(jsonData) {
gearSketch.board = window.gearsketch.model.Board.
fromObject(JSON.parse(jsonData));
}
activity.setup(read, write);
And move the error handling to sugar-web? See what I have here: https://github.com/manuq/gears-activity/blob/master/js/activity.js#L14
I'm also unconvinced we need a write method. I'd rather have activities deal directly with pause and stop, it's more flexible and it adds very little complexity.
Hm that would be more flexible but also more complex for activity developers. I think is better to consistently write on pause and stop in all activities, is more predictable. By the way, I'm happy to open a discussion in the mailing list.
Sorry for the delay :( I somehow missed the notification for this one
No problem at all!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably make sense to post a proposal to the mailing list, it's one of the most important APIs. Up to you though, if you haven't posted tomorrow I will reply here. Don't quite have enough brain energy at the moment to think hard about this :)
Should call onStop too?