We have a large Marionette App, that uses Backbone.trackit
to monitor unsaved changes in our Models.
We now have some nested models, in fact we have a Model, with a Collection of Models, that contain a Collection of Models.
trackit
doesn't support the top level model being marked as 'dirty' when the child models change - due to backbone not bubbling these change events.
I know we could manually monitor these change events, but Im looking for a generic solution.
Has anyone had any experience of the following libs or any other solutions for this?
- backbone-deep-model
- Backbone Associations events
- Custom Backbone.Model.set override that bubbles change events
The immediate requirement is to get trackit
working with nested events - but I cant find any branches to trackit
that add this.
So I was wondering if anyone has approached this, or used the above libs in conjunction with trackit
?
Ideally, if a library would trigger a standard 'change' event all the way up the chain, then trackit
would just pick up on this and start working.
so, if model.countries[3].regions[4].name
changed, a change:countries
event would be triggered on model
. Thus if the model had trackit
enbaled, it would all just work!
The events from models inside a collection already bubbles to the collection by default. So that's one thing solved and we just need to bubble events from a collection or another model inside a model.
One problem I see with bubbling events as-is up the hierarchy is that you then get false positive when you want to listen to that specific collection or model.
A way to avoid that is to namespace events that are bubbling up, but that may not work with trackit.
A simple implementation of a model that enables bubbling up events of arbitrary collections or other nested models:
and to use it:
Any events from
collection
will be prefixed by its optionalkey
or by the defaultnested
string. Achange:myAttribute
event triggered bycollection
would be:Proof of concept with simple tests: