Skip to content

Commit

Permalink
Close #486 move defineModelProperties to Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed May 8, 2017
1 parent 3924938 commit ed5f6b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ <h3>Output</h3>
<script src="../src/public.js"></script>
<script src="../src/data.js"></script>
<script src="../src/template.js"></script>
<script src="../src/model.js"></script>
<script src="../src/utils.js"></script>
<script src="../src/model.js"></script>
<script src="../src/jquery.js"></script>
<script src="../src/plugins/bt-checkbox/plugin.js"></script>
<script src="../src/plugins/bt-selectpicker/plugin.js"></script>
Expand Down
44 changes: 3 additions & 41 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,6 @@ $.extend(Model.prototype, /** @lends Model.prototype */ {
}
});

/**
* Defines properties on an Node prototype with getter and setter.<br>
* Update events are emitted in the setter through root Model (if any).<br>
* 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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/not-group/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]';

Expand Down
38 changes: 38 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,41 @@ Utils.groupSort = function(items, key) {

return newItems;
};

/**
* Defines properties on an Node prototype with getter and setter.<br>
* Update events are emitted in the setter through root Model (if any).<br>
* 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);
}
}
});
});
};
2 changes: 1 addition & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<script src="../src/public.js" data-cover></script>
<script src="../src/data.js" data-cover></script>
<script src="../src/template.js" data-cover></script>
<script src="../src/model.js" data-cover></script>
<script src="../src/utils.js" data-cover></script>
<script src="../src/model.js" data-cover></script>
<script src="../src/jquery.js" data-cover></script>
<script src="../src/plugins/bt-checkbox/plugin.js" data-cover></script>
<script src="../src/plugins/bt-selectpicker/plugin.js" data-cover></script>
Expand Down

0 comments on commit ed5f6b4

Please sign in to comment.