Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

global 'change' doesn't work #28

Closed
cnlevy opened this Issue · 6 comments

2 participants

cnlevy Dhruva Ray
cnlevy

it is important to note that you can't listen to associated model changes just by having
this.on('change', callback);
You need to listen to this.on('change:associatedModel', callback)

Dhruva Ray
Owner

Can you please give an example or a jsfiddle - which demonstrates the problem you are facing? See this particular test case (highlighted in yellow) which does both change and change:AssociatedModel

cnlevy

here is the jsfiddle : http://jsfiddle.net/GV4Tq
I'm talking about the case where you do :
model.get('associatedModel').set(newAttributes)

Dhruva Ray
Owner

Thanks for clarifying. This is deliberate. The rationale for introducing fully qualified paths was that the path would give you info about which node in the hierarchy has changed and you could get the info about the changed node through the event arguments.

We had deliberated about introducing the global change event - at higher levels of the object graph - a couple of times, but settled for fully qualified paths at the end. While it is not difficult to add the change you want, there are some concerns

  1. It would lead to a proliferation of events. In a deeply nested model, for each layer you would have to introduce at least 2 new events : change + change:attribute-name where attribute-name would not be a fully qualified path, but an immediate attribute of the node at that level.

  2. We would have to simulate the events mentioned in pt#1. When you change an attribute in product, you are not really changing the memory reference to product in item. Consequently we cannot delegate (leverage) Backbone base infrastructure to treat it as a change event. Which means we would have to write more custom code and the compactness of the implementation suffers.

  3. We wouldn't want these events to bubble up the hierarchy beyond one level. As it would lead to potentially duplicate events. (As they would also get converted into fully qualified event names at higher levels)

Nonetheless, I will update the pitfalls section of the documentation to make this explicit.

cnlevy
Dhruva Ray
Owner

I have updated (4324af7) the pitfalls section of the documentation and cross referenced this issue. I am glad to hear that the extension is useful to your work. If it's possible, do share a URL. I can put up a usage list if I hear from other folks as well. And yes, don't forget to star us if you like the extension!

Dhruva Ray dhruvaray reopened this
Dhruva Ray
Owner

With BB-Associations v0.5.0+, you can now use the nested-change event for global events. See your revised fiddle here. For rationale, you can see the nested-change section in the event catalogue documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.