diff --git a/public/js/models/answer.js b/public/js/models/answer.js index d8a980d..7fab235 100755 --- a/public/js/models/answer.js +++ b/public/js/models/answer.js @@ -1,6 +1,9 @@ -define(['app', 'moment'], function(App, moment) { +define([ + 'app', + 'models/related-timestamps-model', +], function(App, RelatedTimestampsModel) { App.module('Answer', function(Answer, App, Backbone, Marionette, $, _) { - Answer.Model = Backbone.Model.extend({ + Answer.Model = RelatedTimestampsModel.extend({ defaults: { 'description': '' }, @@ -16,12 +19,6 @@ define(['app', 'moment'], function(App, moment) { this.attachLocalDates(); this.on('sync', this.attachLocalDates); - }, - attachLocalDates: function () { - var updatedLocal = moment.utc(this.get('updated_at')).toDate(); - this.set('updated_local', moment(updatedLocal).format('MMMM Do YYYY, h:mm:ss a')); - var createdLocal = moment.utc(this.get('created_at')).toDate(); - this.set('created_local', moment(createdLocal).format('MMMM Do YYYY, h:mm:ss a')); } }); @@ -55,7 +52,6 @@ define(['app', 'moment'], function(App, moment) { addAnswer: function (model) { var defer = $.Deferred(); - model.set('created_at', moment().format('MMMM Do YYYY, h:mm:ss a')); if (!model.save([], { wait: true, diff --git a/public/js/models/comment.js b/public/js/models/comment.js index 523646e..276bb5e 100644 --- a/public/js/models/comment.js +++ b/public/js/models/comment.js @@ -1,6 +1,9 @@ -define(['app', 'moment'], function(App, moment) { +define([ + 'app', + 'models/related-timestamps-model' +], function(App, RelatedTimestampsModel) { App.module('Comment', function(Comment, App, Backbone, Marionette, $, _) { - Comment.Model = Backbone.Model.extend({ + Comment.Model = RelatedTimestampsModel.extend({ defaults: { 'text': '' }, @@ -13,14 +16,9 @@ define(['app', 'moment'], function(App, moment) { this.urlRoot = App.prefix + '/api/v1/questions/' + options.question_id + '/comments'; + this.attachLocalDates(); this.on('sync', this.attachLocalDates); - }, - attachLocalDates: function () { - var updatedLocal = moment.utc(this.get('updated_at')).toDate(); - this.set('updated_local', moment(updatedLocal).format('MMMM Do YYYY, h:mm:ss a')); - var createdLocal = moment.utc(this.get('created_at')).toDate(); - this.set('created_local', moment(createdLocal).format('MMMM Do YYYY, h:mm:ss a')); } }); @@ -41,7 +39,6 @@ define(['app', 'moment'], function(App, moment) { var API = { addComment: function (model) { var defer = $.Deferred(); - model.set('created_at', moment().format('MMMM Do YYYY, h:mm:ss a')); if (!model.save([], { wait: true, diff --git a/public/js/models/question.js b/public/js/models/question.js index 7d42b2a..532de23 100755 --- a/public/js/models/question.js +++ b/public/js/models/question.js @@ -1,6 +1,10 @@ -define(['app', 'paginator', 'moment'], function(App, PageableCollection, moment) { +define([ + 'app', + 'paginator', + 'models/related-timestamps-model', +], function(App, PageableCollection, RelatedTimestampsModel) { App.module('Question', function(Question, App, Backbone, Marionette, $, _) { - Question.Model = Backbone.Model.extend({ + Question.Model = RelatedTimestampsModel.extend({ urlRoot: App.prefix + '/api/v1/questions', validation: { title: { @@ -15,12 +19,6 @@ define(['app', 'paginator', 'moment'], function(App, PageableCollection, moment) initialize: function (options) { this.attachLocalDates(); this.on('sync', this.attachLocalDates); - }, - attachLocalDates: function () { - var updatedLocal = moment.utc(this.get('updated_at')).toDate(); - this.set('updated_local', moment(updatedLocal).format('MMMM Do YYYY, h:mm:ss a')); - var createdLocal = moment.utc(this.get('created_at')).toDate(); - this.set('created_local', moment(createdLocal).format('MMMM Do YYYY, h:mm:ss a')); } }); @@ -95,6 +93,7 @@ define(['app', 'paginator', 'moment'], function(App, PageableCollection, moment) questionAdd: function (data) { var question = new Question.Model(); var defer = $.Deferred(); + if (!question.save(data, { wait: true, success: function (data) { diff --git a/public/js/models/related-timestamps-model.js b/public/js/models/related-timestamps-model.js new file mode 100644 index 0000000..24120b4 --- /dev/null +++ b/public/js/models/related-timestamps-model.js @@ -0,0 +1,14 @@ +define(['app', 'moment'], function(App, moment) { + App.module('App', function(Answer, App, Backbone, Marionette, $, _) { + App.RelatedTimestampsModel = Backbone.Model.extend({ + attachLocalDates: function () { + var updatedRelative = moment.utc(this.get('updated_at')).toDate(); + this.set('updated_local', moment(updatedRelative).fromNow()); + + var createdRelative = moment.utc(this.get('created_at')).toDate(); + this.set('created_local', moment(createdRelative).fromNow()); + } + }); + }); + return App.RelatedTimestampsModel; +}); diff --git a/public/js/views/comment/composite.js b/public/js/views/comment/composite.js index adae12a..946861c 100644 --- a/public/js/views/comment/composite.js +++ b/public/js/views/comment/composite.js @@ -15,7 +15,6 @@ define(['app', childView: View.SingleCommentCompositeView, childViewContainer: '.comments-region', - events: { 'submit .comments-form': 'submit' },