From 4388b0b37ce45eaf9803aeb8da3b37c320bdd1b5 Mon Sep 17 00:00:00 2001 From: Stefan Lau Date: Wed, 19 Mar 2014 13:11:38 +0100 Subject: [PATCH] add lastUpdated timestamp to all modules --- lib/Module.js | 5 +++++ lib/View.js | 5 ++++- test/specs/lib/Module.js | 9 +++++++++ test/specs/lib/View.js | 2 ++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/Module.js b/lib/Module.js index 037ada7..4b76a65 100644 --- a/lib/Module.js +++ b/lib/Module.js @@ -20,6 +20,11 @@ Module = Backbone.Model.extend({ this.viewId = this.config.viewId; } this.initialize(); + }, + + set: function () { + this.lastUpdated = new Date().getTime(); + Backbone.Model.prototype.set.apply(this, arguments); } }); diff --git a/lib/View.js b/lib/View.js index e3e9738..17a8d1a 100644 --- a/lib/View.js +++ b/lib/View.js @@ -53,8 +53,11 @@ View = Backbone.View.extend({ render: function () { this.updateHtml(); + this.postRender(); }, + postRender: function () {}, + updateHtml: function () { var attrs = _.extend({}, _.result(this, 'attributes'), { 'class': _.result(this, 'className') }), html = !_.isEmpty(this.module.attributes) ? this.renderViewWithData() : this.renderViewWithoutData(); @@ -74,7 +77,7 @@ View = Backbone.View.extend({ return this.compiledTemplate(_.extend({ id: this.module.id, title: this.module.title, - lastUpdated: new Date().getTime() + lastUpdated: this.module.lastUpdated }, this.module.attributes, this.getViewData())); }, diff --git a/test/specs/lib/Module.js b/test/specs/lib/Module.js index 7613f47..978a783 100644 --- a/test/specs/lib/Module.js +++ b/test/specs/lib/Module.js @@ -36,4 +36,13 @@ describe('Module', function () { }); }); + describe('set', function () { + it('should add a lastUpdated timestamp', function () { + var module = new Module({}, {}), + now = new Date().getTime(); + module.set('test', 'attribute'); + expect(module.lastUpdated).to.be.within(now-10, now+10); + }); + }); + }); \ No newline at end of file diff --git a/test/specs/lib/View.js b/test/specs/lib/View.js index bc5dd54..1f1ba0a 100644 --- a/test/specs/lib/View.js +++ b/test/specs/lib/View.js @@ -106,6 +106,7 @@ describe('View', function () { template: 'json', title: 'bar', attributes: { some: 'json', data: 'of', the: 'module' }, + lastUpdated: 123, has: sinon.stub().returns(false) }, view = new View(module); @@ -118,6 +119,7 @@ describe('View', function () { expect(view.compiledTemplate).to.have.been.calledWithMatch({ id: module.id, title: module.title, + lastUpdated: 123, json: JSON.stringify(module.attributes), some: 'json', data: 'of',