Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("DataModule", function(DataModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: Daniel@0: DataModule.addInitializer(function(options){ Daniel@0: Daniel@0: DataModule.ModelWithHashableAttributes = Backbone.Model.extend({ Daniel@0: defaultOptions: { Daniel@0: attributesToExcludeFromHash: null, Daniel@0: customHashSuffixGenerator: null // function(attributes) Daniel@0: }, Daniel@0: Daniel@0: initialize: function(attributes, options) { Daniel@0: this.listenTo(this, "change", this.dropCachedHash); Daniel@0: this.options = _.defaults(options || {}, this.defaultOptions); Daniel@0: }, Daniel@0: Daniel@0: dropCachedHash: function() { Daniel@0: this._cachedHash = undefined; Daniel@0: }, Daniel@0: Daniel@0: getHash: function() { Daniel@0: if (!this._cachedHash) { Daniel@0: var mixOfAttributesAndAttributeHashes = {}; Daniel@0: for (var attributeName in this.attributes) { Daniel@0: if (this.options.attributesToExcludeFromHash && _.contains(this.options.attributesToExcludeFromHash, attributeName)) { Daniel@0: continue; Daniel@0: } Daniel@0: if (this.attributes.hasOwnProperty(attributeName)) { Daniel@0: var attribute = this.attributes[attributeName]; Daniel@0: if (attribute && attribute.getHash) { Daniel@0: mixOfAttributesAndAttributeHashes[attributeName] = attribute.getHash(); Daniel@0: } else { Daniel@0: mixOfAttributesAndAttributeHashes[attributeName] = attribute; Daniel@0: } Daniel@0: } Daniel@0: } Daniel@0: var newHash = JSON.stringify(mixOfAttributesAndAttributeHashes); Daniel@0: if (this.options.customHashSuffixGenerator && _.isFunction(this.options.customHashSuffixGenerator)) { Daniel@0: newHash += this.options.customHashSuffixGenerator(this.attributes); Daniel@0: } Daniel@0: this._cachedHash = newHash; Daniel@0: } Daniel@0: return this._cachedHash; Daniel@0: } Daniel@0: }); Daniel@0: }); Daniel@0: }, Logger);