diff options
author | Struan Donald <struan@exo.org.uk> | 2013-04-08 17:11:57 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2013-04-08 18:55:37 +0100 |
commit | a19459b65a8dbbc3ee12fbf7852956a1b322e3e6 (patch) | |
tree | 51f3a9cf8365e9d74fab63a8acc09fe078598048 | |
parent | bd3a482f3c268c6f4e31c4be0e9b2b8d6045b680 (diff) |
Trying to use the same model for both remote and localStorage was
involving all sorts of hackery so it's easier and cleaner to move to
separate models for reports and drafts
-rw-r--r-- | www/index.html | 1 | ||||
-rw-r--r-- | www/js/app.js | 36 | ||||
-rw-r--r-- | www/js/models/draft.js | 28 | ||||
-rw-r--r-- | www/js/models/report.js | 18 | ||||
-rw-r--r-- | www/js/router.js | 24 | ||||
-rw-r--r-- | www/js/views/submit.js | 12 |
6 files changed, 66 insertions, 53 deletions
diff --git a/www/index.html b/www/index.html index 1f1f77e..b5cc3e0 100644 --- a/www/index.html +++ b/www/index.html @@ -44,6 +44,7 @@ <script type="text/javascript" src="js/views/submit.js"></script> <script type="text/javascript" src="js/views/sent.js"></script> + <script type="text/javascript" src="js/models/draft.js"></script> <script type="text/javascript" src="js/models/report.js"></script> <script type="text/javascript" src="js/models/user.js"></script> diff --git a/www/js/app.js b/www/js/app.js index c8b0d82..8b94fbc 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -47,28 +47,27 @@ var tpl = { currentLocation: null, currentPosition: null, - currentReport: new FMS.Report(), - allReports: new FMS.Reports(), + currentDraft: new FMS.Draft(), + allDrafts: new FMS.Drafts(), reportToView: null, - saveCurrentReport: function() { - this.router.pause(); - this.allReports.add( FMS.currentReport ); - FMS.currentReport.localSave(); - if ( this.currentReport.id ) { - localStorage.currentReportID = this.currentReport.id; - } else { - localStorage.currentReportID = this.currentReport.cid; - } + saveCurrentDraft: function() { + FMS.router.pause(); + FMS.allDrafts.add( FMS.currentDraft ); + FMS.currentDraft.save(); + localStorage.currentDraftID = FMS.currentDraft.cid; }, - loadCurrentReport: function() { + loadCurrentDraft: function() { console.log( 'loading report' ); - if ( localStorage.currentReportID ) { - this.currentReport = FMS.allReports.get( localStorage.currentReportID ); + if ( localStorage.currentDraftID && localStorage.currentDraftID != 'null' ) { + var r = FMS.allDrafts.get( localStorage.currentDraftID ); + if ( r ) { + FMS.currentDraft = r; + } } - localStorage.currentReportID = null; + localStorage.currentDraftID = null; }, initialize: function () { @@ -92,10 +91,13 @@ var tpl = { FMS.currentUser = new FMS.User({id: 1}); } - document.addEventListener('pause', function() { FMS.saveCurrentReport(); }, false); - document.addEventListener('resume', function() { FMS.loadCurrentReport(); }, false); + document.addEventListener('pause', function() { FMS.saveCurrentDraft(); }, false); + document.addEventListener('resume', function() { FMS.loadCurrentDraft(); }, false); document.addEventListener('backbutton', function() { FMS.router.back(); }, true); + FMS.allDrafts.fetch(); + FMS.loadCurrentDraft(); + Backbone.history.start(); navigator.splashscreen.hide(); }); diff --git a/www/js/models/draft.js b/www/js/models/draft.js new file mode 100644 index 0000000..3917ba9 --- /dev/null +++ b/www/js/models/draft.js @@ -0,0 +1,28 @@ +(function(FMS, Backbone, _, $) { + _.extend( FMS, { + Draft: Backbone.Model.extend({ + localStorage: new Backbone.LocalStorage(CONFIG.NAMESPACE + '-drafts'), + + defaults: { + lat: 0, + lon: 0, + title: '', + details: '', + may_show_name: '', + category: '', + phone: '', + pc: '', + file: '' + } + }) + }); +})(FMS, Backbone, _, $); + +(function(FMS, Backbone, _, $) { + _.extend( FMS, { + Drafts: Backbone.Collection.extend({ + model: FMS.Draft, + localStorage: new Backbone.LocalStorage(CONFIG.NAMESPACE + '-drafts') + }) + }); +})(FMS, Backbone, _, $); diff --git a/www/js/models/report.js b/www/js/models/report.js index 3215449..341574c 100644 --- a/www/js/models/report.js +++ b/www/js/models/report.js @@ -2,7 +2,6 @@ _.extend( FMS, { Report: Backbone.Model.extend({ urlRoot: CONFIG.FMS_URL + 'report/ajax', - localStorage: new Backbone.LocalStorage(CONFIG.NAMESPACE + '-reports'), defaults: { lat: 0, @@ -16,14 +15,6 @@ file: '' }, - localSave: function() { - var method = 'create'; - if ( this.id ) { - method = 'update'; - } - Backbone.localSync(method, this); - }, - sync: function(method, model, options) { switch (method) { case 'create': @@ -161,12 +152,3 @@ }) }); })(FMS, Backbone, _, $); - -(function(FMS, Backbone, _, $) { - _.extend( FMS, { - Reports: Backbone.Collection.extend({ - model: FMS.Report, - localStorage: new Backbone.LocalStorage(CONFIG.NAMESPACE + '-reports') - }) - }); -})(FMS, Backbone, _, $); diff --git a/www/js/router.js b/www/js/router.js index f3153ba..c259c05 100644 --- a/www/js/router.js +++ b/www/js/router.js @@ -21,8 +21,8 @@ }, pause: function() { - if (this.currentView && this.currentView.updateCurrentReport) { - this.currentView.updateCurrentReport(); + if (this.currentView && this.currentView.updateCurrentDraft) { + this.currentView.updateCurrentDraft(); } }, @@ -33,52 +33,52 @@ }, around: function(){ - var aroundView = new FMS.AroundView({ model: FMS.currentReport }); + var aroundView = new FMS.AroundView({ model: FMS.currentDraft }); this.changeView(aroundView); }, search: function(){ - var searchView = new FMS.SearchView({ model: FMS.currentReport, msg: FMS.searchMessage }); + var searchView = new FMS.SearchView({ model: FMS.currentDraft, msg: FMS.searchMessage }); this.changeView(searchView); }, home: function(){ - var homeView = new FMS.HomeView({ model: FMS.currentReport }); + var homeView = new FMS.HomeView({ model: FMS.currentDraft }); this.changeView(homeView); }, photo: function(){ - var photoView = new FMS.PhotoView({ model: FMS.currentReport }); + var photoView = new FMS.PhotoView({ model: FMS.currentDraft }); this.changeView(photoView); }, details: function(){ - var detailsView = new FMS.DetailsView({ model: FMS.currentReport }); + var detailsView = new FMS.DetailsView({ model: FMS.currentDraft }); this.changeView(detailsView); }, submit: function(){ - var submitView = new FMS.SubmitView({ model: FMS.currentReport }); + var submitView = new FMS.SubmitView({ model: FMS.currentDraft }); this.changeView(submitView); }, submitEmail: function(){ - var submitEmailView = new FMS.SubmitEmailView({ model: FMS.currentReport }); + var submitEmailView = new FMS.SubmitEmailView({ model: FMS.currentDraft }); this.changeView(submitEmailView); }, submitName: function(){ - var submitNameView = new FMS.SubmitNameView({ model: FMS.currentReport }); + var submitNameView = new FMS.SubmitNameView({ model: FMS.currentDraft }); this.changeView(submitNameView); }, submitPassword: function(){ - var submitPasswordView = new FMS.SubmitPasswordView({ model: FMS.currentReport }); + var submitPasswordView = new FMS.SubmitPasswordView({ model: FMS.currentDraft }); this.changeView(submitPasswordView); }, sent: function(){ - var sentView = new FMS.SentView({ model: FMS.currentReport }); + var sentView = new FMS.SentView({ model: FMS.currentDraft }); this.changeView(sentView); }, diff --git a/www/js/views/submit.js b/www/js/views/submit.js index 8faf32b..b48461c 100644 --- a/www/js/views/submit.js +++ b/www/js/views/submit.js @@ -15,11 +15,6 @@ 'click #submit_register': 'onClickSubmit' }, - initialize: function() { - this.listenTo(this.model, 'sync', this.onReportSync ); - this.listenTo( this.model, 'error', this.onReportError ); - }, - render: function(){ if ( !this.template ) { console.log('no template to render'); @@ -40,7 +35,10 @@ if ( this.validate() ) { this.model.set('user', FMS.currentUser); - this.model.save(); + this.report = new FMS.Report( this.model.toJSON() ); + this.listenTo( this.report, 'sync', this.onReportSync ); + this.listenTo( this.report, 'error', this.onReportError ); + this.report.save(); } }, @@ -48,6 +46,8 @@ if ( FMS.currentUser ) { FMS.currentUser.save(); } + FMS.currentDraft = new FMS.Draft(); + FMS.createdReport = this.report; this.navigate( 'sent', 'left' ); }, |