为了响应MongoDB更新,我在meteor UI中更新了一个d3.js图表。出于某种原因,我手动对本地MongoDB所做的更改在图表中反映出来之前,需要5到10秒的时间。
以下是代码,也许有人会找出延迟的原因:
Template.diagram.rendered = function(){
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
x = d3.scale.ordinal()
.domain('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''))
.rangeRoundBands([0, width], .1);
y = d3.scale.linear()
.domain([0,0.15])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10, "%");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," +
margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
this.autorun(function(){
var data = Letters.find().fetch();
if (!data.length){
return;
}
var bars = svg
.selectAll(".bar")
.data(data, function(d){return d._id;});
bars.enter()
.append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("height",0)
.attr("y", height)
.transition()
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height -
y(d.frequency); });
bars
.transition()
.duration(200)
.ease("sin-out")
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height -
y(d.frequency); });
});
};发布于 2016-02-28 09:42:18
一旦我在meteor论坛上发布了这个问题,我就被指向了2014年的一篇meteor博客文章,其中描述了oplog尾随功能,以及它只在dev实例中默认打开的事实。这让我意识到,通过在我的开发应用程序中使用MONGO_URL环境变量,我强制我的meteor应用程序与MongoDB实例一起工作,该实例一直在我的mac上运行,独立于我的meteor开发,因此,被我的meteor应用程序视为“生产”。一旦我将应用程序切换到使用ad-hock mongo connection / db,oplog结尾就生效了,我开始看到即时的事件传播到浏览器。感谢来自流星论坛的@dburles!
https://stackoverflow.com/questions/35664527
复制相似问题