Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /* List view is already a component */
  3.  
  4. ListView = Vue.component('list-view', (
  5. {
  6.   data: function() {
  7.     return { entrylist: this.$parent.entrylist } // BAD IDEA: see appdata below...
  8.   },
  9.   template: '<div class="two columns"><ul><list-item v-for="item in entrylist" :key="item.id" :idnum="item.id" :name="item.name"></list-item></ul></div>',
  10. }
  11. ))
  12.  
  13.  
  14. /* nice component */
  15. Vue.component( 'list-item', (
  16.   {
  17.     props: [ 'idnum', 'name'],
  18.     computed: {
  19.       addr: function() {
  20.         return "#/item/" + this.idnum;
  21.       }
  22.     },
  23.     template: '<li><a :href="addr">{{name}}</a></li>'
  24.   }
  25. ))
  26.  
  27. const Add = Vue.component('add-view', ({
  28.  
  29.   data: function() {
  30.     return { entrylist: this.$parent.entrylist } // NOTE BAD IDEA HERE TOO: see below
  31.   },
  32.   methods: {
  33.     addNew: function() {
  34.         /*
  35.         var reg = new RegExp("[.]+[@][a-z0-9]+[\.][a-z]+")
  36.         if (this.NewName == "") {
  37.             console.log("name empty")
  38.         }
  39.         if (!this.NewMail.match(reg)) {
  40.             console.log("email incorrect")
  41.         }
  42.                              name:this.NewName, email:this.NewMail})
  43.         */
  44.         this.entrylist.push({id:this.entrylist.length, name:this.NewName, email:this.NewMail})
  45.     }
  46.   },
  47.   template:
  48.   `
  49.   <div>
  50.   <h2>Lisamine</h2>
  51.   <form>
  52.   <input type="text" id="name" placeholder="Name" v-model="NewName"/>
  53.   <br/>
  54.   <input type="text" id="email" placeholder="Email" v-model="NewMail"/>
  55.   <br/>
  56.   <input type="submit" @click="addNew"/>
  57.   </form>
  58.   </div>
  59.   `
  60. }
  61. ))
  62.  
  63. /*Detail view component*/
  64. DetailView = Vue.component( 'detail-view', (
  65.     {
  66.         template:
  67.   `
  68.     <div>
  69.         <h1>Details  {{$route.params.id}}</h1>
  70.         <div>
  71.             {{$route.params.name}}
  72.             <br/>
  73.             {{$route.params.email}}
  74.         </div>
  75.     </div>
  76.    
  77.   `
  78.   }
  79. ))
  80.  
  81. /* APPLICATION STATE */
  82.  
  83. /*
  84. NOTE: bad idea to use it for sharing data directly. We should
  85. be sharing parts of it to components as needed and we should
  86. be changing it using events since it will be a debugging hell
  87. for anything non-trivial.
  88. */
  89.  
  90. const appstate =
  91.     {
  92.       entrylist: [
  93.         { id:0, name:"asd", email:"xd@xd.dx"},
  94.         { id:1, name:"dsa", email:"dx@xd.dx"}
  95.       ]
  96.     }
  97.  
  98.  
  99. /* ROUTER ELEMENTS */
  100. routes = [
  101.   { path: '/list', component: ListView },
  102.   { path: '/add', component: Add },
  103.   { path: '/item/:id', component: DetailView } // change into a proper component
  104. ]
  105.  
  106. const router = new VueRouter(
  107.   {routes, linkActiveClass: "button-primary"}
  108. )
  109.  
  110. /* VUE APP */
  111.  
  112. var vm = new Vue(
  113. {
  114.   router,
  115.   data: appstate
  116. }
  117. ).$mount('#dogapp');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement