From 84d5b027b216ebdb71cdd70b3b8b4eda17b45b54 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Mon, 20 Mar 2017 00:12:32 +0100 Subject: [PATCH] Cast function inside script property to string --- src/demo.js | 6 ++++++ src/dom_components/model/Component.js | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/demo.js b/src/demo.js index 2c3d00d7a..07575763c 100644 --- a/src/demo.js +++ b/src/demo.js @@ -414,6 +414,7 @@ require(['config/require-config'], function() { switchTime: 3000, // Switch time animSpeed: 600, // Animation Speed/Duration droppable: false, + script : function () {console.log(5);}, traits: [{ type: 'number', label: 'Duration', @@ -433,6 +434,11 @@ require(['config/require-config'], function() { }), init: function () { + this.listenTo(this, 'change:status',function(){ + var obj = this.toJSON(); + console.log(obj); + console.log(obj.script); + }); this.listenTo(this, 'change:animSpeed change:autoRotation change:switchTime', this.buildScript); }, diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index 9869e74c3..7d301080d 100644 --- a/src/dom_components/model/Component.js +++ b/src/dom_components/model/Component.js @@ -290,6 +290,27 @@ define(['backbone','./Components', 'SelectorManager/model/Selectors', 'TraitMana return attr; }, + /** + * Return a shallow copy of the model's attributes for JSON + * stringification. + * @return {Object} + * @private + */ + toJSON: function() { + var obj = Backbone.Model.prototype.toJSON.apply(this, arguments); + var script = this.get('script'); + + // Need to cast script functions to string + if (typeof script == 'function') { + var scrStr = script.toString(); + scrStr = scrStr.replace(/^function\s?\(\)\s?\{/, ''); + scrStr = scrStr.replace(/\}$/, ''); + obj.script = scrStr; + } + + return obj; + }, + },{ /**