Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip prototype properties when looping over objects in objToPaths() #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Example code:
console.log(val);
});

//... and all parent attributes as well
model.bind('change:user.name', function(model, val) { console.log(val); });
model.bind('change:user', function(model, val) { console.log(val); });

//Wildcards are supported
model.bind('change:user.*', function() {});

Expand Down
23 changes: 23 additions & 0 deletions distribution/deep-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,17 @@
separator = DeepModel.keyPathSeparator;

for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue;

var val = obj[key];

if (val && val.constructor === Object && !_.isEmpty(val)) {
//Recursion for embedded objects
var obj2 = objToPaths(val);

for (var key2 in obj2) {
if (!obj2.hasOwnProperty(key2)) continue;

var val2 = obj2[key2];

ret[key + separator + key2] = val2;
Expand Down Expand Up @@ -346,6 +350,25 @@
}
//</custom code>
}
//<custom code>
// Flag triggered events so that they are not triggered more then once.
var triggered = {};
_.each(changes, function(key) {

var fields = key.split(separator);

//Trigger change events for parent keys without wildcard (*) notation.

for(var n = fields.length - 1; n > 0; n--) {
var parentKey = _.first(fields, n).join(separator);

if (!triggered[parentKey]) {
this.trigger('change:' + parentKey, this, getNested(current, parentKey), options);
}
triggered[parentKey] = true;
}
}, this);
//</custom code>
}

if (changing) return this;
Expand Down
2 changes: 1 addition & 1 deletion distribution/deep-model.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/deep-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
separator = DeepModel.keyPathSeparator;

for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue;

var val = obj[key];

if (val && val.constructor === Object && !_.isEmpty(val)) {
//Recursion for embedded objects
var obj2 = objToPaths(val);

for (var key2 in obj2) {
if (!obj2.hasOwnProperty(key2)) continue;

var val2 = obj2[key2];

ret[key + separator + key2] = val2;
Expand Down Expand Up @@ -225,6 +229,25 @@
}
//</custom code>
}
//<custom code>
// Flag triggered events so that they are not triggered more then once.
var triggered = {};
_.each(changes, function(key) {

var fields = key.split(separator);

//Trigger change events for parent keys without wildcard (*) notation.

for(var n = fields.length - 1; n > 0; n--) {
var parentKey = _.first(fields, n).join(separator);

if (!triggered[parentKey]) {
this.trigger('change:' + parentKey, this, getNested(current, parentKey), options);
}
triggered[parentKey] = true;
}
}, this);
//</custom code>
}

if (changing) return this;
Expand Down
4 changes: 4 additions & 0 deletions test/deep-model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ test("set: Triggers model change:[attribute] events", function() {
'change:user.name.first',
'change:user.name.*',
'change:user.*',
'change:user.name',
'change:user',
'change'
]);
})();
Expand Down Expand Up @@ -503,6 +505,8 @@ test("unset: Triggers model change:[attribute] events", function() {
'change:user.name.first',
'change:user.name.*',
'change:user.*',
'change:user.name',
'change:user',
'change'
]);
})();
Expand Down