结合Bomb制作交互网站

一、前言

  1. 在前端开发往往只能做一些死的页面,没有数据交互效果。若想搭一个动态网站需要后端技术或后端的工程师辅助开发。
  2. 本文将结合 Bomb 后端云、jQuery 开发一个小型的动态网站。

二、Bomb 后端云介绍

  1. 官网:

    https://www.bmob.cn/

  2. 特点

    1. 轻松搭建应用数据库,支持可视化操作
    2. 支持各种文件上传和存储,CDN 加速更快更稳定
    3. 可在线编写和调试代码,让逻辑实现更加灵活容易
    4. 定时任务,以一定间隔自动完成指定动作
    5. 自定义应用站点,打造属于自己的应用宣传网站
  3. 支持功能

  4. 请注册 Bomb 后使用。

三、创建项目及数据表

  1. 创建项目

    1. 在 Bomb 官网顶部可进入 Bomb 控制台

    2. 在控制台中创建项目

    3. 这里小编事先以把项目创建好,下面的代码将以上图最上面的 “Web” 项目为主。

  2. 创建数据表

    1. 点击云数据库进入 “Web” 项目。

    2. 第一次进入项目后,系统会默认有一个 _User 的表,不用管它,我们自己玩。

    3. 点击左上角的添加表按钮,可创建新的数据表。

    4. 在这里创建一个 shop 表,用来存储数据。这里要注意一下,数据表的名字要严格遵守提示的命名规则,否则无法创建。

    5. 数据表创建成功后,默认会有几个字段(如下图),这些字段不可以删除。同时,在页面的上边有一排操作数据表的功能按钮,接线来我们用这些按钮创建一个我们想要的表。

  3. 添加字段

    1. 表结构

    2. 根据表结构添加列

  4. 添加数据

    1. 这里我们在京东上找一些商品,把数据存进去。

    2. 这里小编存放了 20 条数据。

四、创建 web 项目

  1. 下载文件

    1. 下载 jquery.js 和 Bomb.js 文件
    2. 文件下载地址:https://www.aliyundrive.com/s/2Hm7xbTLFhs
  2. 创建文件

    1. 创建 index.html、index.css、index.js、tool.js 文件。

    2. 将 jquery.js 和 Bomb.js 文件放到项目中。

    3. 在 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>
      

      目录结构

  3. tool.js 文件

    1. tool.js 文件是一个 JS 工具类文件。这个文件主要写一些数据库增删改查的代码。
    2. 在 index.html 中不需要引入。

五、开发 tool 工具类

  1. 初始化文件

    1. 在 index.js 文件中对 Bomb 进行初始化。同时引入 tool 类。

      Bmob.initialize("你的Secret Key", "你的API 安全码");
      import {tool} from './tool.js';
      
    2. 你的 Secret Key 在 Bomb 平台的设置 – 引用秘钥中可以找到,如下图。

    3. 你的 API 安全码在设置 – 安全验证中可以自定义,如下图。

    4. 初始化 tool.js 文件

      // 工具类
      let tool = {
      
      }
      
      export {
          tool
      }
      
  2. 创建测试表

    1. 在云平台添加一个测试表 test

    2. 表结构:

    3. 平台截图:

  3. 向数据库中添加数据

    1. 编辑 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
      }
      
    2. 在 index.js 文件中调用 add 方法,测试一下。

      1. 代码:

        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)
        })
        
      2. 运行 index.html

      3. 查看 Bomb 平台的 test 表

  4. 修改数据

    1. 编辑 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
      }
      
    2. 在 index.js 文件中调用 edit 方法,测试一下。

      1. 代码:

        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)
        })
        
      2. 运行 index.html

      3. 查看 Bomb 平台的 test 表

  5. 查询数据

    1. 编辑 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
      }
      
    2. 在 index.js 文件中调用 get 方法,测试一下。

      1. 代码:

        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);
        		})
        	}
        })
        
      2. 运行 index.html

      3. 查看 Bomb 平台的 test 表

  6. 删除数据

    1. 编辑 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
      }
      
    2. 在 index.js 文件中调用 del 方法,测试一下。

      1. 代码:

        Bmob.initialize("你的Secret Key", "你的API 安全码");
        import {tool} from './tool.js';
        
        // 测试 del 删除数据方法
        tool.del('test','ed8aeba879').then(res => {
        	console.log(res)
        })
        
      2. 运行 index.html

      3. 查看 Bomb 平台的 test 表

    3. 以上就是 tool 工具类里四个操作数据库的方法

六、画页面

  1. 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>
    
  2. 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;
    }
    
  3. 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>
    		`)
    	}
        
    })
    
  4. 运行效果

七、小结

  1. 利用 Bomb 开发 Web 是一个不错的选择,Bomb 是云端一体化互联网中间件服务,为我们提供了实时数据与文件存储功能,轻松实现应用“云与端”的数据连通。
  2. Bomb 还提供了很多相关的功能,例如短信验证、云函数等。
  3. 小编在这里推荐大家去玩一玩。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iGma_e

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值