嵌套的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. python --- 04 列表 元组

    一 .列表 在python中使用[]来描述列表, 内部元素用逗号隔开. 对数据类型没有要求 1.列表存在索引和切片. 和字符串是一样的. 2.增删改查操作 1).增加 1. .append(" ...

  2. ol3开发离线地图

    注意:国内地图均经过加密,尤其是百度地图,经过了二次加密,通常情况下都会存在偏差.所以最好是利用地图下载器下载后面带有“无偏差”的地图,否则需要对经纬度进行转化. 1.需要的前端库文件有jquery. ...

  3. topcoder srm 350 div1

    problem1 link 满足$a^{b}\leq5000000,b>1$的数字不多,有2000多个,直接暴力计算能组成哪些数字即可. import java.util.*; import j ...

  4. CentOS7学习记录(工具使用篇)

    一.   远程连接终端中文乱码:如xShell 检查当前系统语言:echo $LANG 查看系统安装语言包:locale ,如果包含zh_CN.UTF-8表示已经安装中文语言.如果没有中文包,使用命令 ...

  5. 2199: [Usaco2011 Jan]奶牛议会 2-sat

    链接 https://www.luogu.org/problemnew/show/P3007 https://www.lydsy.com/JudgeOnline/problem.php?id=2199 ...

  6. hihoCoder week13 最近公共祖先·一

    用的dfs,自下往上搜索一个节点的所有祖先,然后在相应祖先 判断是否是另一个节点的祖先,如果是 就截止,否则继续往上搜索,直到搜索到,或者知道所有的祖先都被扫描完成 #include <bits ...

  7. 题解——CodeForces 438D The Child and Sequence

    题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...

  8. webpack用 babel将ES6转译ES5

    webpack webpack.config.js配置文件 module.exports = { entry: './es6.js', // 入口文件路径 output: { filename: &q ...

  9. [ECharts] - ECharts使用中国地图

    格式1: https://www.cnblogs.com/luna666/p/9007263.html  (非官方) <!DOCTYPE html> <html lang=" ...

  10. 【C#】取整函数Math.Round、Math.Ceiling和Math.Floor区别

    Math.Round 原则: 四舍六入五取偶. 也就是说 0.1-0.4为0 0.5为0 0.6-0.9为1 1.5为2 Math.Ceiling 原则: 有小数就加1 0.1 = 1 Math.Fl ...