2 (function(angular
, $, _
) {
4 angular
.module('crmAutosave', CRM
.angRequires('crmAutosave'));
7 // var autosave = new CrmAutosaveCtrl({
8 // save: function -- A function to handle saving. Should return a promise.
9 // If it's not a promise, then we'll assume that it completes successfully.
10 // saveIf: function -- Only allow autosave when conditional returns true. Default: !form.$invalid
11 // model: object|function -- (Re)schedule saves based on observed changes to object. We perform deep
12 // inspection on the model object. This could be a performance issue you
13 // had many concurrent autosave forms or a particularly large model, but
14 // it should be fine with typical usage.
15 // interval: object -- Interval spec. Default: {poll: 250, save: 5000}
16 // form: object|function -- FormController or its getter
19 // $scope.$on('$destroy', autosave.stop);
20 // Note: if the save operation itself
21 angular
.module('crmAutosave').service('CrmAutosaveCtrl', function($interval
, $timeout
, $q
) {
22 function CrmAutosaveCtrl(options
) {
23 var intervals
= angular
.extend({poll
: 250, save
: 5000}, options
.interval
);
24 var jobs
= {poll
: null, save
: null}; // job handles used ot cancel/reschedule timeouts/intervals
25 var lastSeenModel
= null;
28 // Determine if model has changed; (re)schedule the save.
29 // This is a bit expensive and doesn't need to be continuous, so we use polling instead of watches.
30 function checkChanges() {
34 var currentModel
= _
.isFunction(options
.model
) ? options
.model() : options
.model
;
35 if (!angular
.equals(currentModel
, lastSeenModel
)) {
36 lastSeenModel
= angular
.copy(currentModel
);
38 $timeout
.cancel(jobs
.save
);
40 jobs
.save
= $timeout(doAutosave
, intervals
.save
);
44 function doAutosave() {
50 var form
= _
.isFunction(options
.form
) ? options
.form() : options
.form
;
53 if (!options
.saveIf()) {
57 else if (form
&& form
.$invalid
) {
62 lastSeenModel
= angular
.copy(_
.isFunction(options
.model
) ? options
.model() : options
.model
);
64 // Set to pristine before saving -- not after saving.
65 // If an eager user continues editing concurrent with the
66 // save process, then the form should become dirty again.
70 var res
= options
.save();
71 if (res
&& res
.then
) {
91 this.start = function() {
93 lastSeenModel
= angular
.copy(_
.isFunction(options
.model
) ? options
.model() : options
.model
);
94 jobs
.poll
= $interval(checkChanges
, intervals
.poll
);
98 this.stop = function() {
100 $interval
.cancel(jobs
.poll
);
104 $timeout
.cancel(jobs
.save
);
109 this.suspend = function(p
) {
111 return p
.finally(self
.start
);
115 return CrmAutosaveCtrl
;
118 })(angular
, CRM
.$, CRM
._
);