In vue.js use timeago format

Discussion in 'JavaScript' started by mstdmstd, Dec 26, 2017.

  1. #1
    Hello,
    In vue.js(2.5) I want to output some properties comma separated, like:

    &nbsp;( {{participantUser.first_name}} {{participantUser.last_name}}, {{participantUser.user_status_label}}, {{participantUser.user_status_label}} )<br>
    Code (markup):
    But as some of the values can be empty, I would like to skip empty comma in resulting string, so I need some routine to deal with empty fields.

    The usual way for this can be to set all parameters in
    
    <div v-if="users_list_block_visible && participantUsersList.length > 0" >
    ...
    {{ participantUserInfo(participantUser) }}
    ...
    </div>
    
    
    methods: {
    
    participantUserInfo(user) {
    var dataArray = [ " ", user['first_name']+' '+user['last_name'], user['user_status_label'], user['visited_at'] ]
    return this.concatStrings(dataArray, ', '); // that is my function for concatinating - ok with it
    ...
    Code (markup):

    The question is that I need field visited_at to show in Since format, using this https://github.com/egoist/vue-timeago library

    When I need to show time of message creation, I do
    <timeago :since="nextUserChatMessage.created_at" :auto-update="60"></timeago>
    Code (markup):
    But as field visited_at can be empty, I aslo need to include it it dataArray array for concatStrings.
    If there is a way to call timeago functionality for visited_at in javascript participantUserInfo function or to make it in some other way?

    I thought about computed methods, but is it this case? computed methods seems do not have parameters....

    I hope I put it clearly....

    Thanks!
     
    mstdmstd, Dec 26, 2017 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    First, you're using an object, the names don't violate JS property/method namespace, just use the object method.

    Second, and I rarely say this... this is a job for hardcoding. ASSUMING the name always exists:

    
    participantUserInfo(user) {
    	var result = user.first_name + ' ' + user.last_name;
    	if (user.user_status_label) result += ', ' + user.user_status_label;
    	if (user.visited_at) result += ', ' + user.visited_at;
    	return result;
    }
    
    Code (markup):
    Sometimes automation will only take you so far.

    Now, that said, IF I were to automate it by making an array, I'd suggest using join instead of concatStrings.
     
    deathshadow, Jan 6, 2018 IP