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

Combine "dateFunction": momentDateFormatter" and"customCallback": populateTpl #77

Open
saturday1 opened this issue Mar 11, 2016 · 6 comments
Labels

Comments

@saturday1
Copy link

I am trying to use the template version (Example 8) combined with moment (Example 6, I think). But then I get problems with the moment js. It does not run the function at all and it does not catch the language moment.locale('sv_SE');. The code looks like this:

    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"></script>

    <script type="text/javascript" src="js/twitterFetcher_min.js"></script>
<script>

moment.locale('sv_SE');

var config = {
    "id": '573452057987973120',
    "dataOnly": true,
    "maxTweets": 3,
    "dateFunction": momentDateFormatter,
    "customCallback": populateTpl
};

function momentDateFormatter(date, dateString) {
    return moment(dateString).fromNow();
}

twitterFetcher.fetch(config);

function populateTpl(tweets){
    var html = '<ul>';
    for (var i = 0, lgth = tweets.length; i < lgth ; i++) {
        var tweetObject = tweets[i];

        html += '<li>'
        + (tweetObject.image ? '<div class="tweet-img"><img src="'+tweetObject.image+'" /></div>' : '')
        + '<p class="tweet-content">' + tweetObject.tweet + '</p>'
        + '<p class="tweet-infos">Posted on the ' + tweetObject.time + ', by ' + tweetObject.author + '</p>'
        + '<p class="tweet-link"><a href="' + tweetObject.permalinkURL + '">Link</a></p>'
        + '</li>';
    }
    html += '</ul>';

    $('#twitter-feed').append(html);
}
</script>

You can see all HTML as well over here: https://cdn.axiell.com/dev/twitter/test/index.html

How do I run moment before the template function is running?

@jasonmayes
Copy link
Owner

I'm afraid I am not a user of moment, but it is possible in the recent Twitter changes the classname changed for the dates which means it may be returning null. Let me check.

@jasonmayes
Copy link
Owner

I can confirm Twitter Fetcher calls the function see this fiddle: https://jsfiddle.net/sxvfeza7/

There must be something not being used right with Moment in that case which is outside the scope of this project.

@saturday1
Copy link
Author

Yes, above example works as intended, also over here (I have similar solution already in place). It is only when I use "dataOnly" it stops working. See the fiddle here which represents my code: https://jsfiddle.net/sxvfeza7/3/. As you can see it does not write anything in console.log. When I turn "dataonly" to false, it does work with dateFormatter.

@jasonmayes jasonmayes reopened this Mar 17, 2016
@jasonmayes
Copy link
Owner

Ah, I see. I shall have a look at that.

@jasonmayes jasonmayes added the bug label Mar 22, 2016
@CoalaWeb
Copy link

CoalaWeb commented May 19, 2017

I'm not sure if it helps but I submitted a pull request for this type of situation in the past as I found the dataonly option wasn’t allowing me to format the date. I forked the Fiddle @saturday1 created and you can see my changes from line 297-320 (open to improvement)

I can then format the date with out needing to load moment using some like this:

function dateFormatter(date) { var options = { weekday: "short", year: "numeric", month: "short", day: "numeric" }; return date.toLocaleDateString("sv-SE", options); }

I'm using it with the CMS Joomla and I load in the current language being used so the dates are formated as needed.

Cheers,

@eballeste
Copy link

eballeste commented Sep 28, 2018

when setting dataOnly to true, you can modify the raw data from the callback function

customCallback: (data) => {
  const convertedData = data.map( (post) => {
    const { timestamp } = post;
    const unixTimestamp = new Date(timestamp).getTime() / 1000;
    post.timestamp = unixTimestamp;
    
    return post;
  });

   // do something with convertedData
}

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

No branches or pull requests

4 participants