一、前言
- 在前端开发往往只能做一些死的页面,没有数据交互效果。若想搭一个动态网站需要后端技术或后端的工程师辅助开发。
- 本文将结合 Bomb 后端云、jQuery 开发一个小型的动态网站。
二、Bomb 后端云介绍
-
官网:
-
特点
- 轻松搭建应用数据库,支持可视化操作
- 支持各种文件上传和存储,CDN 加速更快更稳定
- 可在线编写和调试代码,让逻辑实现更加灵活容易
- 定时任务,以一定间隔自动完成指定动作
- 自定义应用站点,打造属于自己的应用宣传网站
-
支持功能
-
请注册 Bomb 后使用。
三、创建项目及数据表
-
创建项目
-
在 Bomb 官网顶部可进入 Bomb 控制台
-
在控制台中创建项目
-
这里小编事先以把项目创建好,下面的代码将以上图最上面的 “Web” 项目为主。
-
-
创建数据表
-
点击云数据库进入 “Web” 项目。
-
第一次进入项目后,系统会默认有一个 _User 的表,不用管它,我们自己玩。
-
点击左上角的添加表按钮,可创建新的数据表。
-
在这里创建一个 shop 表,用来存储数据。这里要注意一下,数据表的名字要严格遵守提示的命名规则,否则无法创建。
-
数据表创建成功后,默认会有几个字段(如下图),这些字段不可以删除。同时,在页面的上边有一排操作数据表的功能按钮,接线来我们用这些按钮创建一个我们想要的表。
-
-
添加字段
-
表结构
-
根据表结构添加列
-
-
添加数据
-
这里我们在京东上找一些商品,把数据存进去。
-
这里小编存放了 20 条数据。
-
四、创建 web 项目
-
下载文件
- 下载 jquery.js 和 Bomb.js 文件
- 文件下载地址:https://www.aliyundrive.com/s/2Hm7xbTLFhs
-
创建文件
-
创建 index.html、index.css、index.js、tool.js 文件。
-
将 jquery.js 和 Bomb.js 文件放到项目中。
-
在 index.html 文件中引入这几个文件,注意引入的顺序。
index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bomb商城</title> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script src="js/Bomb.js" type="text/javascript" charset="utf-8"></script> <script src="js/index.js" type="module" charset="utf-8"></script> <link rel="stylesheet" type="text/css" href="css/index.css"/> </head> <body> </body> </html>
目录结构
-
-
tool.js 文件
- tool.js 文件是一个 JS 工具类文件。这个文件主要写一些数据库增删改查的代码。
- 在 index.html 中不需要引入。
五、开发 tool 工具类
-
初始化文件
-
在 index.js 文件中对 Bomb 进行初始化。同时引入 tool 类。
Bmob.initialize("你的Secret Key", "你的API 安全码"); import {tool} from './tool.js';
-
你的 Secret Key 在 Bomb 平台的设置 – 引用秘钥中可以找到,如下图。
-
你的 API 安全码在设置 – 安全验证中可以自定义,如下图。
-
初始化 tool.js 文件
// 工具类 let tool = { } export { tool }
-
-
创建测试表
-
在云平台添加一个测试表 test
-
表结构:
-
平台截图:
-
-
向数据库中添加数据
-
编辑 tool.js 文件
// 工具类 let tool = { /** * 添加数据 * @param {string} table 表名 * @param {Array} data 数据,对象数组,key-字段名,value-字段值 * 例:data: [{key: 'name',value: 'Bmob'},{key: 'phone',value: '13122223333'}] */ add: (table,data) => new Promise((t,f) => { const query = Bmob.Query(table); for(let prop of data) query.set(prop.key,prop.value); query.save().then(res => { t(true); }) }), } export { tool }
-
在 index.js 文件中调用 add 方法,测试一下。
-
代码:
Bmob.initialize("你的Secret Key", "你的API 安全码"); import {tool} from './tool.js'; // 测试 add 添加数据方法 let data = [ { key: 'name',value: '张三' }, { key: 'age',value: 20 }, ]; tool.add('test',data).then(res => { console.log(res) })
-
运行 index.html
-
查看 Bomb 平台的 test 表
-
-
-
修改数据
-
编辑 tool.js 文件
// 工具类 let tool = { /** * 添加数据 * @param {string} table 表名 * @param {Array} data 数据,对象数组,key-字段名,value-字段值 * 例:data: [{key: 'name',value: 'Bmob'},{key: 'phone',value: '13122223333'}] */ add: (table,data) => new Promise((t,f) => { const query = Bmob.Query(table); for(let prop of data) query.set(prop.key,prop.value); query.save().then(res => { t(true); }) }), /** * 修改数据 * @param {string} table 表名 * @param {*} id 字段ID * @param {Array} data 数据,key-字段名,value-字段值 */ edit: (table,id,data) => new Promise((t,f) => { const query = Bmob.Query(table); query.set('id', id) for(let prop of data) query.set(prop.key,prop.value); query.save().then(res => { t(true); }) }), } export { tool }
-
在 index.js 文件中调用 edit 方法,测试一下。
-
代码:
Bmob.initialize("你的Secret Key", "你的API 安全码"); import {tool} from './tool.js'; // 测试 edit 修改数据方法 let data = [ { key: 'name',value: '李四' }, { key: 'age',value: 40 }, ]; tool.edit('test','ba29605a05',data).then(res => { console.log(res) })
-
运行 index.html
-
查看 Bomb 平台的 test 表
-
-
-
查询数据
-
编辑 tool.js 文件
// 工具类 let tool = { /** * 添加数据 * @param {string} table 表名 * @param {Array} data 数据,对象数组,key-字段名,value-字段值 * 例:data: [{key: 'name',value: 'Bmob'},{key: 'phone',value: '13122223333'}] */ add: (table,data) => new Promise((t,f) => { const query = Bmob.Query(table); for(let prop of data) query.set(prop.key,prop.value); query.save().then(res => { t(true); }) }), /** * 修改数据 * @param {string} table 表名 * @param {*} id 字段ID * @param {Array} data 数据,key-字段名,value-字段值 */ edit: (table,id,data) => new Promise((t,f) => { const query = Bmob.Query(table); query.set('id', id) for(let prop of data) query.set(prop.key,prop.value); query.save().then(res => { t(true); }) }), /** * 查询数据 * @param {string} table 表名 * @param {array} data 条件 [{key: 字段名,rela: 条件(> <……),value: 值}] * @param {string} order 排序字段 */ get: (table,data=[],order='') => new Promise((t,f) => { const query = Bmob.Query(table); for(let prop of data) query.equalTo(prop.key,prop.rela,prop.value); query.order(order); query.find().then(res => { t(res) }); }) } export { tool }
-
在 index.js 文件中调用 get 方法,测试一下。
-
代码:
Bmob.initialize("你的Secret Key", "你的API 安全码"); import {tool} from './tool.js'; // 在测试表里添加第二条数据 let data = [ { key: 'name',value: '张三' }, { key: 'age',value: 20 }, ]; tool.add('test',data).then(res => { if(res) { // 测试 get 获取数据方法 tool.get('test',[ { key: 'age',rela: '<',value: 30 } ]).then(res => { console.log(res); }) } })
-
运行 index.html
-
查看 Bomb 平台的 test 表
-
-
-
删除数据
-
编辑 tool.js 文件
// 工具类 let tool = { /** * 添加数据 * @param {string} table 表名 * @param {Array} data 数据,对象数组,key-字段名,value-字段值 * 例:data: [{key: 'name',value: 'Bmob'},{key: 'phone',value: '13122223333'}] */ add: (table,data) => new Promise((t,f) => { const query = Bmob.Query(table); for(let prop of data) query.set(prop.key,prop.value); query.save().then(res => { t(true); }) }), /** * 删除数据 * @param {string} table 表名 * @param {*} id 字段ID */ del: (table,id) => new Promise((t,f) => { const query = Bmob.Query(table); query.destroy(id).then(res => { t(true); }) }), /** * 修改数据 * @param {string} table 表名 * @param {*} id 字段ID * @param {Array} data 数据,key-字段名,value-字段值 */ edit: (table,id,data) => new Promise((t,f) => { const query = Bmob.Query(table); query.set('id', id) for(let prop of data) query.set(prop.key,prop.value); query.save().then(res => { t(true); }) }), /** * 查询数据 * @param {string} table 表名 * @param {array} data 条件 [{key: 字段名,rela: 条件(> <……),value: 值}] * @param {string} order 排序字段 */ get: (table,data=[],order='') => new Promise((t,f) => { const query = Bmob.Query(table); for(let prop of data) query.equalTo(prop.key,prop.rela,prop.value); query.order(order); query.find().then(res => { t(res) }); }) } export { tool }
-
在 index.js 文件中调用 del 方法,测试一下。
-
代码:
Bmob.initialize("你的Secret Key", "你的API 安全码"); import {tool} from './tool.js'; // 测试 del 删除数据方法 tool.del('test','ed8aeba879').then(res => { console.log(res) })
-
运行 index.html
-
查看 Bomb 平台的 test 表
-
-
以上就是 tool 工具类里四个操作数据库的方法
-
六、画页面
-
index.html 文件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bomb商城</title> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script src="js/Bomb.js" type="text/javascript" charset="utf-8"></script> <script src="js/index.js" type="module" charset="utf-8"></script> <link rel="stylesheet" type="text/css" href="css/index.css"/> </head> <body> <div class="box"> <ul class="list"></ul> </div> </body> </html>
-
index.css 文件
* { margin: 0; padding: 0; } li { list-style: none; } .wh { width: 100%; height: 100%; } .box { width: 1600px; height: 900px; position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%); overflow: auto; padding: 10px; box-sizing: border-box; border-radius: 10px; box-shadow: 0 0 10px 0 #e0ddd6; } .box::-webkit-scrollbar { border-width:1px; } .list { display: flex; justify-content: space-between; flex-wrap: wrap; } .item { width: 300px; height: 400px; margin-bottom: 10px; padding: 10px; box-sizing: border-box; border: 1px solid #e1ddd6; border-radius: 10px; } .item img { width: 100%; } .title { width: 100%; margin: 10px 0; text-overflow: -o-ellipsis-lastline; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; line-clamp: 2; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } .price { color: red; font-weight: bold; font-size: 22px; }
-
index.js 文件
Bmob.initialize("你的Secret Key", "你的API 安全码"); import {tool} from './tool.js'; // 利用工具类里的 get 方法获取数据 tool.get('shop').then(res => { // 利用 jquery 将数据渲染到页面中 for(let prop of res) { $(".list").append(` <li class="item"> <img src="${prop.image}" > <div class="title"> <p>${prop.title}</p> </div> <p class="price">${prop.price}</p> </li> `) } })
-
运行效果
七、小结
- 利用 Bomb 开发 Web 是一个不错的选择,Bomb 是云端一体化互联网中间件服务,为我们提供了实时数据与文件存储功能,轻松实现应用“云与端”的数据连通。
- Bomb 还提供了很多相关的功能,例如短信验证、云函数等。
- 小编在这里推荐大家去玩一玩。