Skip to content
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
19 changes: 19 additions & 0 deletions src/deep-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,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