之前有过介绍跨平台的移动开发框架---Ionic+Cordova,它属于HybirdApp开发模式,可以很快很方便的开发出跨平台的移动应用。移动应用总是要同服务器端进行数据交互的,而跨平台的移动应用开发者大多是前端开发人员。如果要学习php,java,asp.net来创建网站或服务来提供数据,学习成本高,周期长。这就是Bmob出现的理由之一,为你的移动应用提供后端云服务,它声称使用Bmob几乎能免去所有服务器端编码工作量,前端工程师几分钟内即可为你的APP添加一个强大的后端服务系统。如果是后端人员转的前端和移动开发,或者想学习后端的同学,那么可以忽视这篇文章了,我相信你宁愿自己搭建服务器。如果是前端的同学,而且想快速开发一个CRUD功能的应用,那么这篇文章值得一看。
<script src="lib/bmob/bmob-min.js"></script>
在app.js中的config函数里对SDK进行初始化:
Bmob.initialize("你的Application ID", "你的REST API Key");
var GameScore = Bmob.Object.extend("GameScore");//GameScore是表名 var gameScore = new GameScore(); gameScore.set("score", 1337); gameScore.save(null, { success: function(object) { alert("create object success, object id:"+object.id); }, error: function(model, error) { alert("create object fail"); } });
var GameScore = Bmob.Object.extend("GameScore"); var query = new Bmob.Query(GameScore); query.get("4edc3f6ee9", { success: function(object) { // The object was retrieved successfully. alert(object.get("score")); }, error: function(object, error) { alert("query object fail"); } });
var GameScore = Bmob.Object.extend("GameScore"); var query = new Bmob.Query(GameScore); query.get("4edc3f6ee9", { success: function(object) { // The object was retrieved successfully. object.set("score", 1338); object.save(null, { success: function(objectUpdate) { alert("create object success, object score:"+objectUpdate.get("score")); }, error: function(model, error) { alert("create object fail"); } }); }, error: function(object, error) { alert("query object fail"); } });
var GameScore = Bmob.Object.extend("GameScore"); var query = new Bmob.Query(GameScore); query.get("4edc3f6ee9", { success: function(object) { // The object was retrieved successfully. object.destroy({ success: function(deleteObject) { alert("delete success"); }, error: function(GameScoretest, error) { alert("delete fail"); } }); }, error: function(object, error) { alert("query object fail"); } });
更多的开发资料请参考官网的开发文档,这里给出链接: