From a19459b65a8dbbc3ee12fbf7852956a1b322e3e6 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 8 Apr 2013 17:11:57 +0100 Subject: 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 --- www/js/app.js | 36 +++++++++++++++++++----------------- www/js/models/draft.js | 28 ++++++++++++++++++++++++++++ www/js/models/report.js | 18 ------------------ www/js/router.js | 24 ++++++++++++------------ www/js/views/submit.js | 12 ++++++------ 5 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 www/js/models/draft.js (limited to 'www/js') 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' ); }, -- cgit v1.2.3