Skip to content

Commit ed5f6b4

Browse files
committed
Close mistic100#486 move defineModelProperties to Utils
1 parent 3924938 commit ed5f6b4

File tree

6 files changed

+45
-45
lines changed

6 files changed

+45
-45
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ module.exports = function(grunt) {
2222
'src/public.js',
2323
'src/data.js',
2424
'src/template.js',
25-
'src/model.js',
2625
'src/utils.js',
26+
'src/model.js',
2727
'src/jquery.js'
2828
],
2929
js_files_for_standalone: [

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ <h3>Output</h3>
133133
<script src="../src/public.js"></script>
134134
<script src="../src/data.js"></script>
135135
<script src="../src/template.js"></script>
136-
<script src="../src/model.js"></script>
137136
<script src="../src/utils.js"></script>
137+
<script src="../src/model.js"></script>
138138
<script src="../src/jquery.js"></script>
139139
<script src="../src/plugins/bt-checkbox/plugin.js"></script>
140140
<script src="../src/plugins/bt-selectpicker/plugin.js"></script>

src/model.js

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,44 +64,6 @@ $.extend(Model.prototype, /** @lends Model.prototype */ {
6464
}
6565
});
6666

67-
/**
68-
* Defines properties on an Node prototype with getter and setter.<br>
69-
* Update events are emitted in the setter through root Model (if any).<br>
70-
* The object must have a `__` object, non enumerable property to store values.
71-
* @param {function} obj
72-
* @param {string[]} fields
73-
*/
74-
Model.defineModelProperties = function(obj, fields) {
75-
fields.forEach(function(field) {
76-
Object.defineProperty(obj.prototype, field, {
77-
enumerable: true,
78-
get: function() {
79-
return this.__[field];
80-
},
81-
set: function(value) {
82-
var previousValue = (this.__[field] !== null && typeof this.__[field] == 'object') ?
83-
$.extend({}, this.__[field]) :
84-
this.__[field];
85-
86-
this.__[field] = value;
87-
88-
if (this.model !== null) {
89-
/**
90-
* After a value of the model changed
91-
* @event model:update
92-
* @memberof Model
93-
* @param {Node} node
94-
* @param {string} field
95-
* @param {*} value
96-
* @param {*} previousValue
97-
*/
98-
this.model.trigger('update', this, field, value, previousValue);
99-
}
100-
}
101-
});
102-
});
103-
};
104-
10567

10668
/**
10769
* Root abstract object
@@ -177,7 +139,7 @@ var Node = function(parent, $el) {
177139
this.parent = parent;
178140
};
179141

180-
Model.defineModelProperties(Node, ['level', 'error', 'data', 'flags']);
142+
Utils.defineModelProperties(Node, ['level', 'error', 'data', 'flags']);
181143

182144
Object.defineProperty(Node.prototype, 'parent', {
183145
enumerable: true,
@@ -340,7 +302,7 @@ var Group = function(parent, $el) {
340302
Group.prototype = Object.create(Node.prototype);
341303
Group.prototype.constructor = Group;
342304

343-
Model.defineModelProperties(Group, ['condition']);
305+
Utils.defineModelProperties(Group, ['condition']);
344306

345307
/**
346308
* Removes group's content
@@ -561,7 +523,7 @@ var Rule = function(parent, $el) {
561523
Rule.prototype = Object.create(Node.prototype);
562524
Rule.prototype.constructor = Rule;
563525

564-
Model.defineModelProperties(Rule, ['filter', 'operator', 'value']);
526+
Utils.defineModelProperties(Rule, ['filter', 'operator', 'value']);
565527

566528
/**
567529
* Checks if this Node is the root

src/plugins/not-group/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ QueryBuilder.define('not-group', function(options) {
104104
* @memberof Group
105105
* @instance
106106
*/
107-
Model.defineModelProperties(Group, ['not']);
107+
Utils.defineModelProperties(Group, ['not']);
108108

109109
QueryBuilder.selectors.group_not = QueryBuilder.selectors.group_header + ' [data-not=group]';
110110

src/utils.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,41 @@ Utils.groupSort = function(items, key) {
202202

203203
return newItems;
204204
};
205+
206+
/**
207+
* Defines properties on an Node prototype with getter and setter.<br>
208+
* Update events are emitted in the setter through root Model (if any).<br>
209+
* The object must have a `__` object, non enumerable property to store values.
210+
* @param {function} obj
211+
* @param {string[]} fields
212+
*/
213+
Utils.defineModelProperties = function(obj, fields) {
214+
fields.forEach(function(field) {
215+
Object.defineProperty(obj.prototype, field, {
216+
enumerable: true,
217+
get: function() {
218+
return this.__[field];
219+
},
220+
set: function(value) {
221+
var previousValue = (this.__[field] !== null && typeof this.__[field] == 'object') ?
222+
$.extend({}, this.__[field]) :
223+
this.__[field];
224+
225+
this.__[field] = value;
226+
227+
if (this.model !== null) {
228+
/**
229+
* After a value of the model changed
230+
* @event model:update
231+
* @memberof Model
232+
* @param {Node} node
233+
* @param {string} field
234+
* @param {*} value
235+
* @param {*} previousValue
236+
*/
237+
this.model.trigger('update', this, field, value, previousValue);
238+
}
239+
}
240+
});
241+
});
242+
};

tests/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
<script src="../src/public.js" data-cover></script>
3939
<script src="../src/data.js" data-cover></script>
4040
<script src="../src/template.js" data-cover></script>
41-
<script src="../src/model.js" data-cover></script>
4241
<script src="../src/utils.js" data-cover></script>
42+
<script src="../src/model.js" data-cover></script>
4343
<script src="../src/jquery.js" data-cover></script>
4444
<script src="../src/plugins/bt-checkbox/plugin.js" data-cover></script>
4545
<script src="../src/plugins/bt-selectpicker/plugin.js" data-cover></script>

0 commit comments

Comments
 (0)