[Vuejs]-Vuex and Laravel 5: how to remove Tags from Articles

0đź‘Ť

âś…

Personally I like to use ID’s to reference any database resource aswell as keeping my objects in javascript somewhat the same as my API.

1

In this case I would have changed my tags to objects instead of strings and send an ID of the tag to my API.

An example of my article would look like:

let article = {
    tags: [{ id: 1, name: 'tag 1' }, { id: 2 ... }]
}

Using objects or IDs as parameters are in my opinion both fine. I should stick with objects if you like “cleaner” code, there will only be one place to check if the ID is present in your object aswell as the selection of the ID.

Case:

// Somehwere in code
this.deleteArticle(article);

// Somehwere else in code
this.deleteArticle(article);

// Somewhere else in code
this.deleteArticle(article);

// Function
deleteArticle(article) {
  // This check or other code will only be here instead of 3 times in code
  if (!article.hasOwnProperty('id')) {
    console.error('No id!');
  }

  let id = article.id;

  ...
}

2

Normally I would keep the logic of changing variables in the components where they are first initialized. So where you first tell this.article = someApiData;. Have a function in there that handles the final removal of the deleted tag.

3

If you are looking for ultimate world domination performance I would remove the tag in javascript. You could also just send the updated list of tags back in the response and update all tags of the article with this data and keep your code slim. However I still like to slice() the deleted tag from the array.

Remember this is my opinion. Your choises are completely fine and you should, like I do myself, never stop questioning yours and others code.

Leave a comment