Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 App.module("DataModule", function(DataModule, App, Backbone, Marionette, $, _, Logger) {
|
Daniel@0
|
4
|
Daniel@0
|
5 DataModule.addInitializer(function(options){
|
Daniel@0
|
6
|
Daniel@0
|
7 DataModule.ModelWithHashableAttributes = Backbone.Model.extend({
|
Daniel@0
|
8 defaultOptions: {
|
Daniel@0
|
9 attributesToExcludeFromHash: null,
|
Daniel@0
|
10 customHashSuffixGenerator: null // function(attributes)
|
Daniel@0
|
11 },
|
Daniel@0
|
12
|
Daniel@0
|
13 initialize: function(attributes, options) {
|
Daniel@0
|
14 this.listenTo(this, "change", this.dropCachedHash);
|
Daniel@0
|
15 this.options = _.defaults(options || {}, this.defaultOptions);
|
Daniel@0
|
16 },
|
Daniel@0
|
17
|
Daniel@0
|
18 dropCachedHash: function() {
|
Daniel@0
|
19 this._cachedHash = undefined;
|
Daniel@0
|
20 },
|
Daniel@0
|
21
|
Daniel@0
|
22 getHash: function() {
|
Daniel@0
|
23 if (!this._cachedHash) {
|
Daniel@0
|
24 var mixOfAttributesAndAttributeHashes = {};
|
Daniel@0
|
25 for (var attributeName in this.attributes) {
|
Daniel@0
|
26 if (this.options.attributesToExcludeFromHash && _.contains(this.options.attributesToExcludeFromHash, attributeName)) {
|
Daniel@0
|
27 continue;
|
Daniel@0
|
28 }
|
Daniel@0
|
29 if (this.attributes.hasOwnProperty(attributeName)) {
|
Daniel@0
|
30 var attribute = this.attributes[attributeName];
|
Daniel@0
|
31 if (attribute && attribute.getHash) {
|
Daniel@0
|
32 mixOfAttributesAndAttributeHashes[attributeName] = attribute.getHash();
|
Daniel@0
|
33 } else {
|
Daniel@0
|
34 mixOfAttributesAndAttributeHashes[attributeName] = attribute;
|
Daniel@0
|
35 }
|
Daniel@0
|
36 }
|
Daniel@0
|
37 }
|
Daniel@0
|
38 var newHash = JSON.stringify(mixOfAttributesAndAttributeHashes);
|
Daniel@0
|
39 if (this.options.customHashSuffixGenerator && _.isFunction(this.options.customHashSuffixGenerator)) {
|
Daniel@0
|
40 newHash += this.options.customHashSuffixGenerator(this.attributes);
|
Daniel@0
|
41 }
|
Daniel@0
|
42 this._cachedHash = newHash;
|
Daniel@0
|
43 }
|
Daniel@0
|
44 return this._cachedHash;
|
Daniel@0
|
45 }
|
Daniel@0
|
46 });
|
Daniel@0
|
47 });
|
Daniel@0
|
48 }, Logger);
|