github presentation of minimongo将其声明为
客户端mongo数据库,服务器通过http同步
还有一个minimongo-standalone提供了一个minimongo.min.js,其中说明:
你也可以下载minimongo.min.js,把它放在你的 服务器,并在您的来源中引用它。
对于浏览器
<script src="/js/minimongo.min.js"></script>
我之前使用的d3.js是以某种方式打包的,因此.js文件在Web浏览器中作为lib工作,在节点上作为npm包工作。
所以我尝试使用新下载的minimongo.js
本地在Chrome中使用indexeddb
构建一个经典网页,就像我对D3.js一样。它提供了类似的东西(jsfiddle):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MiniMongo</title>
<script src="https://rawgit.com/rurri/minimongo-standalone/master/minimongo.min.js"></script>
<!-- https://github.com/rurri/minimongo-standalone/blob/master/minimongo.min.js -->
</head>
<body></body>
<script>
// Require minimongo
var minimongo = require("minimongo");
var LocalDb = minimongo.MemoryDb;
// Create local db (in memory database with no backing)
db = new LocalDb();
// Add a collection to the database
db.addCollection("animals");
doc = { species: "dog", name: "Bingo" };
// Always use upsert for both inserts and modifies
db.animals.upsert(doc, function() {
// Query dog (with no query options beyond a selector)
db.animals.findOne({
species: "dog"
}, {}, function(res) {
console.log("Dog's name is: " + res.name);
});
});
</script>
</html>
&#13;
它返回错误:
Uncaught ReferenceError: _ is not defined
at minimongo.min.js:1
at minimongo.min.js:3
Uncaught ReferenceError: require is not defined
at (index):5911
Uncaught ReferenceError: _ is not defined
at (index):91
at window.onload ((index):5889)
我错过了什么或误解了什么? 如果可能,如何使它工作?
答案 0 :(得分:4)
一些事情
如果您阅读README.MD
的{{1}},则说明
要求
下划线,异步
因此,您需要在minimongo-standalone
脚本标记之前在页面上包含这两个库。
minimongo
值得一提的是,您需要获取这些库的浏览器版本。似乎<head>
<script src="https://link/to/underscore.js"></script>
<script src="https://link/to/async.js"></script>
<script src="https://rawgit.com/rurri/minimongo-standalone/master/minimongo.min.js"></script>
没有使用通用模块定义(UMD),因此为不同的目标提供单独的文件。
除非您使用browserify或其他commonJS模块加载框架,否则函数async
不存在。
我还没有检查require
或async
,但如果模块系统不存在,大多数库都会回退到浏览器中的普通全局变量。
在包含三个脚本标记后,您应该能够全局访问minimongo-standalone的导出符号
underscore
与minimongo-standalone
令人沮丧的一点; minimongo
实现围绕minimongo-standalone
的{{1}}包装器,然后再次重命名它们。
这意味着任何Meteor
或minimongo
代码都不能直接转让。
优点是API更简单。您的示例的等效代码将是
minimongo
答案 1 :(得分:1)
@Fred_Stark:我们鼓励您将其整合到您的答案中。
短:工作但超重(800kb!)。 https://jsfiddle.net/7fehe9ey/9/。使用别的东西!
/* **************************************************************** */
/* THIS WILL GO TO BUNDLE ***************************************** */
// Require minimongo
var minimongo = require('minimongo');
var LocalDb = minimongo.MemoryDb;
// Create local db (in memory database with no backing)
db = new LocalDb();
/* **************************************************************** */
/* THIS WILL GO TO INDEX.HTML ************************************* */
// Add a collection to the database
db.addCollection("animals");
doc = { species: "dog", name: "Bingo" };
// Always use upsert for both inserts and modifies
db.animals.upsert(doc, function() {
// Query dog (with no query options beyond a selector)
db.animals.findOne({
species: "dog"
}, {}, function(res) {
console.log("Dog's name is: " + res.name);
});
});
npm install minimongo underscore async
npm install -g browserify
echo "var minimongo=require('minimongo');var LocalDb=minimongo.MemoryDb;db=new LocalDb();" > ./app.js
browserify ./app.js -o ./bundle.js
它产生一个800Kb ./bundle.js文件,在jsfiddle中用作黑盒子jsfiddle.net/7fehe9ey/8
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MiniMongo</title>
<!-- ======================================================= -->
<!-- ======================================================= -->
<script src="./js/bundle.js"></script><!--================== -->
<!-- ======================================================= -->
<!-- ======================================================= -->
</head>
<body>
</body>
<script>
// Require minimongo
// var minimongo = require('minimongo');
// var LocalDb = minimongo.MemoryDb;
// Create local db (in memory database with no backing)
// db = new LocalDb();
// Add a collection to the database
db.addCollection("animals");
doc = { species: "Cat", name: "Cingo" };
// Always use upsert for both inserts and modifies
db.animals.upsert(doc, function() {
// Query dog (with no query options beyond a selector)
db.animals.findOne({
species: "Cat"
}, {}, function(res) {
console.log("Cat's name is: " + res.name);
});
});
</script>
</html>
控制台返回:
猫的名字是:Cingo