diff --git a/Gruntfile.js b/Gruntfile.js index bdb40cc2..cfb31c00 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -22,8 +22,8 @@ module.exports = function(grunt) { 'src/public.js', 'src/data.js', 'src/template.js', - 'src/model.js', 'src/utils.js', + 'src/model.js', 'src/jquery.js' ], js_files_for_standalone: [ diff --git a/examples/index.html b/examples/index.html index ff6592b0..369c9eda 100644 --- a/examples/index.html +++ b/examples/index.html @@ -133,8 +133,8 @@

Output

- + diff --git a/src/model.js b/src/model.js index af13aac7..a397c98f 100644 --- a/src/model.js +++ b/src/model.js @@ -64,44 +64,6 @@ $.extend(Model.prototype, /** @lends Model.prototype */ { } }); -/** - * Defines properties on an Node prototype with getter and setter.
- * Update events are emitted in the setter through root Model (if any).
- * The object must have a `__` object, non enumerable property to store values. - * @param {function} obj - * @param {string[]} fields - */ -Model.defineModelProperties = function(obj, fields) { - fields.forEach(function(field) { - Object.defineProperty(obj.prototype, field, { - enumerable: true, - get: function() { - return this.__[field]; - }, - set: function(value) { - var previousValue = (this.__[field] !== null && typeof this.__[field] == 'object') ? - $.extend({}, this.__[field]) : - this.__[field]; - - this.__[field] = value; - - if (this.model !== null) { - /** - * After a value of the model changed - * @event model:update - * @memberof Model - * @param {Node} node - * @param {string} field - * @param {*} value - * @param {*} previousValue - */ - this.model.trigger('update', this, field, value, previousValue); - } - } - }); - }); -}; - /** * Root abstract object @@ -177,7 +139,7 @@ var Node = function(parent, $el) { this.parent = parent; }; -Model.defineModelProperties(Node, ['level', 'error', 'data', 'flags']); +Utils.defineModelProperties(Node, ['level', 'error', 'data', 'flags']); Object.defineProperty(Node.prototype, 'parent', { enumerable: true, @@ -340,7 +302,7 @@ var Group = function(parent, $el) { Group.prototype = Object.create(Node.prototype); Group.prototype.constructor = Group; -Model.defineModelProperties(Group, ['condition']); +Utils.defineModelProperties(Group, ['condition']); /** * Removes group's content @@ -561,7 +523,7 @@ var Rule = function(parent, $el) { Rule.prototype = Object.create(Node.prototype); Rule.prototype.constructor = Rule; -Model.defineModelProperties(Rule, ['filter', 'operator', 'value']); +Utils.defineModelProperties(Rule, ['filter', 'operator', 'value']); /** * Checks if this Node is the root diff --git a/src/plugins/not-group/plugin.js b/src/plugins/not-group/plugin.js index 6596a817..da6f98b0 100644 --- a/src/plugins/not-group/plugin.js +++ b/src/plugins/not-group/plugin.js @@ -104,7 +104,7 @@ QueryBuilder.define('not-group', function(options) { * @memberof Group * @instance */ -Model.defineModelProperties(Group, ['not']); +Utils.defineModelProperties(Group, ['not']); QueryBuilder.selectors.group_not = QueryBuilder.selectors.group_header + ' [data-not=group]'; diff --git a/src/utils.js b/src/utils.js index 8f6150f2..94af0956 100644 --- a/src/utils.js +++ b/src/utils.js @@ -202,3 +202,41 @@ Utils.groupSort = function(items, key) { return newItems; }; + +/** + * Defines properties on an Node prototype with getter and setter.
+ * Update events are emitted in the setter through root Model (if any).
+ * The object must have a `__` object, non enumerable property to store values. + * @param {function} obj + * @param {string[]} fields + */ +Utils.defineModelProperties = function(obj, fields) { + fields.forEach(function(field) { + Object.defineProperty(obj.prototype, field, { + enumerable: true, + get: function() { + return this.__[field]; + }, + set: function(value) { + var previousValue = (this.__[field] !== null && typeof this.__[field] == 'object') ? + $.extend({}, this.__[field]) : + this.__[field]; + + this.__[field] = value; + + if (this.model !== null) { + /** + * After a value of the model changed + * @event model:update + * @memberof Model + * @param {Node} node + * @param {string} field + * @param {*} value + * @param {*} previousValue + */ + this.model.trigger('update', this, field, value, previousValue); + } + } + }); + }); +}; diff --git a/tests/index.html b/tests/index.html index 343c9210..3af96d4d 100644 --- a/tests/index.html +++ b/tests/index.html @@ -38,8 +38,8 @@ - +