Commit fa596904 authored by Sean Hammond's avatar Sean Hammond

Move event-listener functions out of init()

parent 8cb8ee78
...@@ -215,26 +215,45 @@ function AnnotationController( ...@@ -215,26 +215,45 @@ function AnnotationController(
*/ */
newlyCreatedByHighlightButton = model.$highlight || false; newlyCreatedByHighlightButton = model.$highlight || false;
$scope.$on('$destroy', function() { // Call `onDestroy()` when this AnnotationController's scope is removed.
updateTimestamp = angular.noop; $scope.$on('$destroy', onDestroy);
});
// Watch for changes to the domain model and recreate the view model when it // Call `onModelChange()` whenever `model` changes.
// changes. $scope.$watch((function() {return model;}), onModelChange, true);
$scope.$watch((function() {return model;}), function(model, old) {
if (model.updated !== old.updated) { // Call `onGroupFocused()` whenever the currently-focused group changes.
// Discard saved drafts. $scope.$on(events.GROUP_FOCUSED, onGroupFocused);
drafts.remove(model);
// New annotations (just created locally by the client, rather then
// received from the server) have some fields missing. Add them.
model.user = model.user || session.state.userid;
model.group = model.group || groups.focused().id;
model.permissions = model.permissions || permissions['default'](model.group);
// Automatically save new highlights to the server when they're created.
// Note that this line also gets called when the user logs in (since
// AnnotationController instances are re-created on login) so serves to
// automatically save highlights that were created while logged out when you
// log in.
saveNewHighlight();
// If this annotation is not a highlight and if it's new (has just been
// created by the annotate button) or it has edits not yet saved to the
// server - then open the editor on AnnotationController instantiation.
if (!newlyCreatedByHighlightButton) {
if (!model.id || drafts.get(model)) {
vm.edit();
}
}
} }
updateTimestamp(model === old); // Repeat on first run. /** Called when this AnnotationController instance's scope is removed. */
vm.render(); function onDestroy() {
}, true); updateTimestamp = angular.noop;
}
// When the current group changes, persist any unsaved changes using /** Called whenever the currently-focused group changes. */
// the drafts service. They will be restored when this annotation is function onGroupFocused() {
// next loaded.
$scope.$on(events.GROUP_FOCUSED, function() {
if (!vm.editing()) { if (!vm.editing()) {
return; return;
} }
...@@ -253,36 +272,22 @@ function AnnotationController( ...@@ -253,36 +272,22 @@ function AnnotationController(
vm.annotation.group = model.group; vm.annotation.group = model.group;
} }
// if we have a draft, update it, otherwise (eg. when the user signs out)
// do not create a new one.
if (drafts.get(model)) { if (drafts.get(model)) {
var draftDomainModel = {}; var draftDomainModel = {};
updateDomainModel(draftDomainModel, vm.annotation); updateDomainModel(draftDomainModel, vm.annotation);
updateDraft(draftDomainModel); updateDraft(draftDomainModel);
} }
});
// New annotations (just created locally by the client, rather then
// received from the server) have some fields missing. Add them.
model.user = model.user || session.state.userid;
model.group = model.group || groups.focused().id;
model.permissions = model.permissions || permissions['default'](model.group);
// Automatically save new highlights to the server when they're created.
// Note that this line also gets called when the user logs in (since
// AnnotationController instances are re-created on login) so serves to
// automatically save highlights that were created while logged out when you
// log in.
saveNewHighlight();
// If this annotation is not a highlight and if it's new (has just been
// created by the annotate button) or it has edits not yet saved to the
// server - then open the editor on AnnotationController instantiation.
if (!newlyCreatedByHighlightButton) {
if (!model.id || drafts.get(model)) {
vm.edit();
} }
/** Called whenever `model` changes. */
function onModelChange(model, old) {
if (model.updated !== old.updated) {
// Discard saved drafts.
drafts.remove(model);
} }
updateTimestamp(model === old); // Repeat on first run.
vm.render();
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment