嵌套的JavaScript评论 Widget Models

创建类似https://disqus.com/ 的插件

交互插件:

  • Real time comments:
  • Adapts your site's lokk and feel,可以自定义的调整界面外观
  • Rich media commenting读者可以增加图片和视频。
  • Works everywhere.支持各种设备,语言。

https://gorails.com/系列视频:Embeddable JS widgets.


1.下载模版,按照vue.js

rails new embeded_comment -m template.rb

rails webpacker:install:vue

2. 创建数据库表格Discussion和Comment.

rails g scaffold Discussion url title comments_count:integer

rails g scaffold Comment discussion:references name email body:text ip_address user_agent

rails db:migrate

解释:

url属性,存储当前的讨论版的网址。

然后修改hello_vue为embed

mv app/javascript/packs/{hello_vue,embed}.js  

添加代码:

let url = window.location.href

#encodeURIComponent()用于对输入的URl部分进行转义

fetch(`http://localhost:3000/api/v1/discussions/${encodeURIComponent(url)}`, {
headers: { accept: 'application/json' }
})
.then(response => response.json())
.then(data => console.log(data))

3. 增加路径routes.rb,然后创建一个controller.

mkdir -p app/controllers/api/v1

touch app/controllers/api/v1/disscussions_controller.rb

改为:

  namespace :api do
namespace :v1 do
resources :discussions
end
end resources :discussions do
resources :comments
end

增加一个controller的show方法:

任何如http://localhost/?a=11之类的网址,会启用emben.js中的代码,然后执行show action行为,并转到对应的网页

class Api::V1::DiscussionsController < ApplicationController
def show
@discussion = Discussion.by_url(params[:id])
render "discussions/show"
end
end

在model,增加一个类方法by_url

#model, 增加by_url类方法。一个sanitize URL的方法,只要"/?"或者“/#”前面的URL部分
#http://localhost/?a=11
#http://localhost:3000/disscussions/#a=shanghai
class Discussion < ApplicationRecord
has_many :comments def self.by_url(url)
uri = url.split("?").first
uri = url.split("#").first
uri.sub!(/\/$/, '')
   # 如果comments中存在这个uri则选择它,不存在则创建它。
where(url: uri).first_or_create
end
end

改动:app/views/discussions/index.html.erb

在最后一行添加:

javascript_pack_tag "embed"

遇到一个问题:

NoMethodError in Devise::SessionsController#create

undefined method `current_sign_in_at' for #<User:0x00007fcad84de6f8>

 
生成User表格时没有使用Trackable下的属性:
      ## Trackable
# t.integer :sign_in_count, default: , null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.string :current_sign_in_ip
# t.string :last_sign_in_ip

2个方法解决:

  • 添加上需要的属性,migration
  • 或者从Devise model中去掉:trackable.

视频2

使用Vuex建立Vue前端和Rails后端的关联

1. 安装Vuex

Vuex是a state management pattern + library。用于Vue.js app。

yarn add vuex

2. 前端vue.js

事件监听:

# embed.js

const event = (typeof Turbolinks == "object" && Turbolinks.supported) ? "turbolinks:load" : "DOMContentLoaded"

document.addEventListener(event, () => {
const el = document.querySelector("#comment")
const app = new Vue({
el,
render: h => h(App)
}) console.log(app)
})

修改app.js

<template>
<div id="comments">
<p>{{ message }}</p>
</div>
</template>

把上一视频的代码移动到store.js中

embed.js载入它。

import store from '../store'

// 使用Vuex关联store.调用store的action中的方法
store.dispatch("loadComments")

解释:Action通过store.dispatch来触发。


几张截图回顾一下Vuex和Vue

1.  比较vue, Vuex实例中的特性:

  • data - state
  • methods - actions/mutations
  • computed - getters

2.Vuex的motion。

  1. axios执行到context.commit,
  2. 执行mutations中的SET_LOADING_STATUS方法,
  3. 然后再对state中的特性进行修改。

3. 执行fetchTodos方法的过程图

  • 首先,执行:commit("SET_LOADING_STATUS", status),  最后State上更新loadingStatus: 'loading'
  • 然后:取数据:axios.get('/api/todos'),
  • 当数据被取回后,执行后面的context.commit。
    • commit('SET_LOADING_STATUS', status),  最后更新loadingStatus: 'notLoading'
    • 最后commit('SET_TODOS', todos), 最后更新State中的todos属性。
  • 最后, 执行this.$store.getters.doneTodo


新建store.js文件:

import Vue from 'vue'
import Vuex from "vuex" Vue.use(Vuex) const store = new Vuex.Store({
state: {
comments: []
}, mutations: {
load(state, comments) {
state.comments = comments
}
}, action: {
// 使用了参数解构。用commit来代替context.commit。
// context其实是一个store实例。
//async异步函数的普通写法:解释见

(GoRails)使用vue和Vuex管理嵌套的JavaScript评论, 使用组件vue-map-field的更多相关文章

  1. 十、Vue:Vuex实现data(){}内数据多个组件间共享

    一.概述 官方文档:https://vuex.vuejs.org/zh/installation.html 1.1vuex有什么用 Vuex:实现data(){}内数据多个组件间共享一种解决方案(类似 ...

  2. 使用vuex管理数据

    src/vuex/store.js 组件里面使用引入store.js,注意路径 import store from 'store.js' 然后在使用的组件内data(){}同级放入store 三大常用 ...

  3. 【09】Vue 之 Vuex 数据通信

    9.1. 引言 Vue组件化做的确实非常彻底,它独有的vue单文件组件也是做的非常有特色.组件化的同时带来的是:组件之间的数据共享和通信的难题. 尤其Vue组件设计的就是,父组件通过子组件的prop进 ...

  4. 应用四:Vue之VUEX状态管理

    (注:本文适用于有一定Vue基础或开发经验的读者,文章就知识点的讲解不一定全面,但却是开发过程中很实用的) 概念:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应 ...

  5. vue项目--vuex状态管理器

    本文取之官网和其他文章结合自己的理解用简单化的语言表达.用于自己的笔记记录,也希望能帮到其他小伙伴理解,学习更多的前端知识. Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态 ...

  6. vue创建状态管理(vuex的store机制)

    1:为什么说要是永远状态管理 在使用 Vue 框架做单页面应用时,我们时常会遇到传值,组件公用状态的问题.(子父间传值文章传送门) ,如果是简单的应用,兄弟组件之间通信还能使用 eventBus 来作 ...

  7. Vue之状态管理(vuex)与接口调用

    Vue之状态管理(vuex)与接口调用 一,介绍与需求 1.1,介绍 1,状态管理(vuex) Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态 ...

  8. Vue笔记:使用 vuex 管理应用状态

    如果你在使用 vue.js , 那么我想你可能会对 vue 组件之间的通信感到崩溃 . 我在使用基于 vue.js 2.0 的UI框架 ElementUI 开发网站的时候 , 就遇到了这种问题 : 一 ...

  9. 转 理解vuex -- vue的状态管理模式

    转自:https://segmentfault.com/a/1190000012015742 vuex是什么? 先引用vuex官网的话: Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 ...

随机推荐

  1. 【python004-分支循环】

    一.条件分支 1.第一个改进要求:游戏猜错的时候程序提示用户当前的输入比答案大了还是小了 python的比较操作符: >         左边大于右边 >=       左边大于等于右边 ...

  2. How to Rerun Failed Tests in JUnit?

    该帖转自其他出处 Sometimes due to some temporarily problems such as connection problems, server problems, br ...

  3. Python3 tkinter基础 LabelFrame StringVar 单击按钮,Label中显示的文字更换

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. 彻底理解mysql服务器的字符集转换问题

    主要参考这三个文章: https://www.xiariboke.com/article/4147.html http://blog.sina.com.cn/s/blog_690c46500100k1 ...

  5. shell中的变量a=100, 什么时候作整数使用, 什么时候作字符串使用呢?

    shell中的变量a=100, 什么时候作整数使用, 什么时候作字符串使用呢? 这确实是一个困扰很久的一个问题? how it can be an issue? 事实上, 在shell中, 你可以认为 ...

  6. shell编程中的单/双 小括号, 中括号, 大括号

    linux shell中的变量类型?分字符串或者数字或者bool类型吗? 参考: http://www.cnblogs.com/nufangrensheng/p/3477281.html 不分! sh ...

  7. 自己网盘的页面生成器(私用公开-Golang)

    虽说我的网盘(exm,也许页面确实丑了点,不过页面生成的样式你自己可以改)美工已经被乱刀砍死,但是还是有小伙伴问我是怎么搭建的 关于搭建 这个真没什么好说的,vps我只安装了nginx,然后配置域名指 ...

  8. html 之 区块元素属性(待补充)

    区块标签:(会随着父类的宽度变化而变化) p,div 等(后续补充) 区块标签才能使用以下属性() width,height,min-height,max-height,min-width,max-h ...

  9. SQL Server (MSSQLSERVER) 服务因 2148081668 服务性错误而停止。

    https://zhidao.baidu.com/question/151448005.html 具体步骤: 运行-> 输入:“services.msc” ->找到 “SQL Server ...

  10. echarts获取数据的一些难点1

    像上面获取数据后,如果再根据下方按钮查询不同获取的价格,虽然曲线价格能按照不同的来展示, 但是问题有: 查询到的company字段虽然在获取的data中能测试出,但是在上方填入这些companys后, ...