{"id":37224,"date":"2018-07-11T03:39:20","date_gmt":"2018-07-11T10:39:20","guid":{"rendered":"https:\/\/www.exoplatform.com\/blog\/?p=16666"},"modified":"2018-07-11T03:39:20","modified_gmt":"2018-07-11T10:39:20","slug":"use-vue-js-hooks-within-exo-portlet","status":"publish","type":"post","link":"https:\/\/www.exoplatform.com\/blog\/use-vue-js-hooks-within-exo-portlet\/","title":{"rendered":"How to use Vue.js hooks within exo portlet"},"content":{"rendered":"<p>In <a href=\"https:\/\/www-upgrade.exoplatform.com\/blog\/2018\/05\/08\/use-vue-js-exo-platform\/\" target=\"_blank\" rel=\"noopener\">my previous post<\/a>, I detailed some techniques for bringing up a few reactive concepts &nbsp;for <a href=\"https:\/\/www.exoplatform.com\/?utm_source=BlogEn&amp;utm_medium=Blog&amp;utm_campaign=Content&amp;utm_content=link\">eXo<\/a>. This post is sort of part 1.1; it\u2019s a continuation on the subject of View.js\u2019s hooks and methods handling. All of the source code discussed in this article is located <a href=\"https:\/\/github.com\/kmenzli\/exo-github-integration\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<h2>More Advanced Concept<\/h2>\n<p>In the previous example, we had a <a href=\"https:\/\/www.visme.co\/form-builder\/\" target=\"_blank\" rel=\"noopener\"><u>simple form<\/u><\/a> with two radio options for choosing which <strong>GitHub branches<\/strong> we want to display commits for. However, it is common for forms to have multiple buttons performing different actions but sharing some input fields. Now let\u2019s try to improve the form with a field that lets you indicate the name of the git branch you want to inspect and a button that loads the commits of this branch from your <strong>GitHub project<\/strong>.<\/p>\n<h2><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-16669 aligncenter\" src=\"https:\/\/www-upgrade.exoplatform.com\/blog\/\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1.png\" alt=\"How to use Vue.js hooks\" width=\"217\" height=\"395\" srcset=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1.png 332w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1-165x300.png 165w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1-260x473.png 260w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1-180x328.png 180w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1-130x236.png 130w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1-72x131.png 72w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1-49x90.png 49w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2018\/07\/how-to-use-vue-js-eXo-Platform-1-16x30.png 16w\" sizes=\"(max-width: 217px) 100vw, 217px\" \/><\/h2>\n<h2>Working with methods<\/h2>\n<p>In any <strong>web application<\/strong> we need to output data based on certain rules or logic; thus, it might be handy to have a function in which we can <strong>embed JavaScript<\/strong> logic. Or perhaps we want to fetch data from a remote service or something similar.<\/p>\n<p>Within <a href=\"https:\/\/www-upgrade.exoplatform.com\/blog\/2018\/05\/08\/use-vue-js-exo-platform\/\" target=\"_blank\" rel=\"noopener\">Vue.js<\/a>, functions have to be added under the <strong>methods<\/strong> property. I see <a href=\"http:\/\/context.reverso.net\/traduction\/anglais-francais\/you%27ve+noticed\" class=\"broken_link\" target=\"_blank\" rel=\"noopener\">you&#8217;ve noticed<\/a> the term \u201cproperty\u201d; well, in <strong>Vue.js<\/strong>, JavaScript functions are called in the context of an object that can operate on the data contained within the object. The object itself is the Vue instance.<\/p>\n<p>The following snippet illustrates how methods in Vue.js is used :<\/p>\n<pre class=\"lang:default decode:true \">new Vue({\nel: '#app',\ndata: {\ncurrentBranch: 'master'\n},\nmethods: {\n\n}\n});\n<\/pre>\n<p>Methods can be used for various things, but for now I want the method to build the full URL to my <strong>GitHub project<\/strong> when I select a branch:<\/p>\n<pre class=\"lang:default decode:true \">new Vue({\nel: '#app',\ndata: {\ncurrentBranch: 'master',\nrepositoryUrl: 'https:\/\/github.com\/blogs\/vuejs'\n},\nmethods: {\nbuildCurrentBranch: function() {\nreturn this.repositoryUrl + ' ' + this.currentBranch;\n}\n}\n});\n<\/pre>\n<p>So by writing <strong>this<\/strong>.<i>repositoryUrl<\/i>, we can access the <i>repositoryUrl<\/i> property within the data object. Note that <strong>this<\/strong> is required. Technically speaking, this concept is called <strong>proxying.<\/strong><\/p>\n<p>We could access other methods from within our buildCurrentBranch method by using the same approach, except with a parenthetical at the end to make it a method invocation.<\/p>\n<p>Alright, so now that we have implemented our method, let\u2019s use it from within our template. This is as easy as you would expect it to be \u2013 simply call your method within a button component or use the mustache pattern as usual:<\/p>\n<pre class=\"lang:default decode:true \">&lt;!-- Button Load--&gt;\n&lt;div&gt;\n         &lt;button type=\"submit\" class=\"btn btn-primary\" v-on:click=\"buildCurrentBranch();\"&gt;Load commits&lt;\/button&gt;\n&lt;\/div&gt;\n<\/pre>\n<p>The controller side looks like this:<\/p>\n<pre class=\"lang:default decode:true \">&lt;!-- Button buildBranch--&gt;\nbuildCurrentBranch: function () {\n   var self = this\n    $.ajax({\n              type: 'GET',\n              url: apiURL.concat(self.currentBranch),\n             success: function (data) {\n                                                     console.log(data);\n                                                    \/\/ Reload project tree;\n                                                    self.commits = data;\n             },\n             error: function (xhr) {\n                                                    if (xhr.status &gt;= 400) {\n                                                                          console.log(xhr.responseText);\n                                                     } else {\n                                                               alert('error while loading commits. Please try again.');\n                                                     }\n            }\n    });\n\n},\n\n<\/pre>\n<p>This summarizes the basics of using methods within the Vue instance and how to invoke them from an HTML template.<\/p>\n<p>Now let\u2019s see how we can combine methods and hooks, but first of all, what\u2019s a hook?<\/p>\n<h2>What are Vues.js hooks ?<\/h2>\n<p>Hooks are an important part of any serious component <strong>framework<\/strong>. You often need to know when your component is created, added to the DOM, updated, or destroyed.<\/p>\n<p>Vue.js comes up with a set of built-in hooks that developers can use in their components:<\/p>\n<ul>\n<li style=\"font-weight: 400;\">BeforeCreated<\/li>\n<li style=\"font-weight: 400;\">Created<\/li>\n<li style=\"font-weight: 400;\">BeforeMount<\/li>\n<li style=\"font-weight: 400;\">Mounted<\/li>\n<li style=\"font-weight: 400;\">BeforeUpdate<\/li>\n<li style=\"font-weight: 400;\">Updated<\/li>\n<li style=\"font-weight: 400;\">BeforeDestroy<\/li>\n<li style=\"font-weight: 400;\">Destroyed<\/li>\n<\/ul>\n<p>Below we will discuss only Created and Destroyed hooks.<\/p>\n<h2>Created hook<\/h2>\n<p>With the created hook, you will be able to access reactive data . The <strong>Virtual DOM<\/strong> has not yet been mounted or rendered. As a developer, this is a good place to implement your business logic layer, such as loading data or initializing fields.<\/p>\n<pre class=\"lang:default decode:true \"> created: function () {\n       this.fetchCommits()\n    }\n<\/pre>\n<h2>Destroyed hook<\/h2>\n<p>This hook allows you to perform actions when your component has been destroyed, such as cleanup or analytics sending. They fire when your component is being torn down and removed from the <strong>DOM<\/strong>.<\/p>\n<p>As a developer, you can think of this hook as a tool to do any last-minute cleanup or inform a remote server that the component was destroyed. Typically, I use such a hook to add some traces in the logs.<\/p>\n<pre class=\"lang:default decode:true \">destroyed: function () {\n     this.branches: null,\n     this.currentBranch: null,\n     this.commits: null\n    }\n\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>Day after day, <strong>Vue.js<\/strong> is continuing to prove that building end-user interfaces is no longer a complicated task for web and back-end developers. This framework brings forth native concepts that have emerged with <strong>HTML5<\/strong>, such as components, shadow DOM, etc.<\/p>\n<p>With Vue.js, front-end developers can build their own components and reuse them in various applications.<\/p>\n<p>In recent blog posts, I have already covered some basic concepts such as two-way binding, hooks, and lifecycles. But there are still other key notions to explore, like mixins, server-side rendering, bootstrap-vue integration, and others.<\/p>\n<p>It has not been our goal in these articles to provide a comprehensive guide. For that, I advise you to take a look at the <a href=\"https:\/\/docs.exoplatform.org\" target=\"_blank\" rel=\"noopener\">official documentation<\/a> available on the <strong>Vue.js<\/strong> website (including samples and resources), or check out the excellent courses on egghead.io and udemy.<\/p>\n<p>I hope these posts have conveyed why I am personally so excited about Vue.js and that they have helped you get up and running with this <strong>framework<\/strong>, so that you can try out some of the supplied material within your own <a href=\"https:\/\/www.exoplatform.com\/?utm_source=BlogEn&amp;utm_medium=Blog&amp;utm_campaign=Content&amp;utm_content=link\">eXo Platform<\/a> instance.<\/p>\n<p>In the following blog post, I will dig into how we can use <strong>Vue.js<\/strong> with webpack to <strong>build and package your web application<\/strong>.<\/p>\n<div>\n<div class=\"adv-events\" style=\"background: #476fad; padding: 30px 20px; color: white;\">\n<div class=\"media\">\n<div class=\"pull-right\"><a href=\"https:\/\/community.exoplatform.com\/portal\/dw\/\" target=\"_blank\" rel=\"noopener\"><br \/>\n<img decoding=\"async\" class=\"size-full wp-image-6587 alignright\" style=\"border: none;\" src=\"https:\/\/www-upgrade.exoplatform.com\/blog\/\/wp-content\/uploads\/2016\/02\/tribe.png\" alt=\"Join The eXo Tribe\" height=\"120px\"><br \/>\n<\/a><\/div>\n<div class=\"media-body\">\n<h4 class=\"media-heading\"><a href=\"https:\/\/community.exoplatform.com\/portal\/dw\/\" target=\"_blank\" rel=\"noopener\">Join The eXo Tribe<\/a><\/h4>\n<p><a href=\"https:\/\/community.exoplatform.com\/portal\/dw\/\" target=\"_blank\" rel=\"noopener\"><br \/>\nRegister for our Community to Get updates, tutorials, support, and access to the Platform and add-on downloads. <strong>Sign in Now!<\/strong><br \/>\n<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"In my previous post, I detailed some techniques for bringing up a few reactive concepts &nbsp;for eXo. This post is sort of part 1.1; it\u2019s a continuation on the subject of View.js\u2019s hooks and methods handling. All of the source code discussed in this article is located here. More Advanced Concept In the previous example, [&hellip;]","protected":false},"author":7,"featured_media":16668,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":[],"categories":[819],"tags":[765,711,767],"lang":"en","translations":{"en":37224},"pll_sync_post":[],"_links":{"self":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts\/37224"}],"collection":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/comments?post=37224"}],"version-history":[{"count":0,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts\/37224\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/media\/16668"}],"wp:attachment":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/media?parent=37224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/categories?post=37224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/tags?post=37224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}