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

Automatically update for dynamically added time elements #22

Open
Globegitter opened this issue Jul 20, 2013 · 1 comment
Open

Automatically update for dynamically added time elements #22

Globegitter opened this issue Jul 20, 2013 · 1 comment

Comments

@Globegitter
Copy link
Contributor

I like the idea of just having to initialize $('body').timeago(); one time globally and then it keeps track of everything automatically but that only seems to work in a limited manner.
I know there is $('body').timeago('refresh'); for scenarios I am talking about but I seem to having issue with my ember.js app (see http://emberjs.com ), which also doesn't seem to be too easy to solve on the ember-side alone.
Basically I have a full JS frontend app, which dynamically loads data from an API. And that is where the issue starts: We have a user-profile, which dynamically loads timestamps from the server for posts. Ember.js doesn't seem to have an event-hook for when it populates the 'datetime' attribute.

To cut straight to the chase, would it be possible to implement some observer that automatically keeps track of programatically inserted/changed attributes at an unknown time?
I would obviously be willing to help, but I am also not quite sure how much time I will have available to do this all by myself.

The things I have found so far, if you are interested:
http://darcyclarke.me/development/detect-attribute-changes-with-jquery/
http://stackoverflow.com/questions/4561845/firing-event-on-dom-attribute-change

https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Events/Mutation_events
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver?redirectlocale=en-US&redirectslug=DOM%2FMutationObserver

http://www.w3.org/TR/DOM-Level-3-Events/

At some of these implementations I am worried about performance (with the setInterval implementation). But in any case, let me know what you think and if I should look a bit more into it.

@philfreo
Copy link

This repo already does a great job IMO with its $('body').timeago() and $('body').timeago('refresh'); methods. I was able to quickly get it to update (in a performant way -- unlike jquery-timeago) on a single page app using Backbone. The key is to just setup your app so that some event is triggered when you render any new view that might contain timeagos, so that you call refresh at those times. For me that looks like:

    app.on('viewRendered', _.debounce(function() {
        $('body').timeago('refresh');
    }, 100));
    $('body').timeago();

(using underscore's debounce to prevent calling timeago more often than necessary if I render lots of views at once)

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

No branches or pull requests

2 participants