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

Global Change Event #57

Open
vlucas opened this issue Jun 26, 2013 · 5 comments
Open

Global Change Event #57

vlucas opened this issue Jun 26, 2013 · 5 comments

Comments

@vlucas
Copy link

vlucas commented Jun 26, 2013

I wrote some custom javascript that runs calculations and total from rows that are rendered in sync, and I need to re-run the calculations when anything on the page changes. I hooked into jQuery's global ajaxComplete to update the calculations for my own AJAX stuff, but I am not finding any kind of similar global syncUpdate, etc. event to listen on for changes to the page from sync. Is there a way this can be added? Like some type of event fired on document that I can listen to for anytime a row is added, updated, or deleted with sync?

@chrismccord
Copy link
Owner

I like the idea of a global change event, but it might get tricky for your binding code if you receive successive change events. For instance, if you have, say 2, sync partials on the page for the same model, if that model pushed out an update, you would receive 2 global change events back-to-back. If you're expecting this, it might not be an issue, but I could see with a heavily sync'd page, a global change event possibly becoming contextless. That said, I still think it is probably worth having, but have you considered rolling this on your own with a Sync.View subclass?

IE:

class Sync.ChangeEmitterView extends Sync.View
  afterUpdate: -> $(document).trigger("sync:update", $el: @$el, name: @name)
  afterInsert: -> $(document).trigger("sync:insert", $el: @$el, name: @name)
  afterRemove: -> $(document).trigger("sync:remove", $el: @$el, name: @name)


class YourModelPartial extends Sync.ChangeEmitterView

thoughts?

@vlucas
Copy link
Author

vlucas commented Jul 1, 2013

It's definitely something I could do on my own with the provided way to customize the JavaScript interactions, but I figured it would be a common enough use-case to build into sync itself.

@ajb
Copy link
Collaborator

ajb commented Oct 8, 2013

👍 this is very common IMO.

@ajb
Copy link
Collaborator

ajb commented Dec 22, 2014

At this point, I'd be more than happy to help review a PR for this feature, especially if addresses @chrismccord's concerns about multiple events being fired in succession.

@chrismccord
Copy link
Owner

With the right docs, I think the change event handling is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants