diff src/DML/MainVisBundle/Resources/assets/marionette/modules/DataModule/DataModule.20-ModelWithHashableAttributes.js @ 0:493bcb69166c

added public content
author Daniel Wolff
date Tue, 09 Feb 2016 20:54:02 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/DML/MainVisBundle/Resources/assets/marionette/modules/DataModule/DataModule.20-ModelWithHashableAttributes.js	Tue Feb 09 20:54:02 2016 +0100
@@ -0,0 +1,48 @@
+"use strict";
+
+App.module("DataModule", function(DataModule, App, Backbone, Marionette, $, _, Logger) {
+
+    DataModule.addInitializer(function(options){
+        
+        DataModule.ModelWithHashableAttributes = Backbone.Model.extend({
+            defaultOptions: {
+                attributesToExcludeFromHash: null,
+                customHashSuffixGenerator: null // function(attributes)
+            },
+            
+            initialize: function(attributes, options) {
+                this.listenTo(this, "change", this.dropCachedHash);
+                this.options = _.defaults(options || {}, this.defaultOptions);
+            },
+            
+            dropCachedHash: function() {
+                this._cachedHash = undefined;
+            },
+
+            getHash: function() {
+                if (!this._cachedHash) {
+                    var mixOfAttributesAndAttributeHashes = {};
+                    for (var attributeName in this.attributes) {
+                        if (this.options.attributesToExcludeFromHash && _.contains(this.options.attributesToExcludeFromHash, attributeName)) {
+                            continue;
+                        }
+                        if (this.attributes.hasOwnProperty(attributeName)) {
+                            var attribute = this.attributes[attributeName];
+                            if (attribute && attribute.getHash) {
+                                mixOfAttributesAndAttributeHashes[attributeName] = attribute.getHash();
+                            } else {
+                                mixOfAttributesAndAttributeHashes[attributeName] = attribute;
+                            }
+                        }
+                    }
+                    var newHash = JSON.stringify(mixOfAttributesAndAttributeHashes);
+                    if (this.options.customHashSuffixGenerator && _.isFunction(this.options.customHashSuffixGenerator)) {
+                        newHash += this.options.customHashSuffixGenerator(this.attributes);
+                    }
+                    this._cachedHash = newHash;
+                }
+                return this._cachedHash;
+            }
+        });
+    });
+}, Logger);