这是对我的理解的检查requestAnimationFrame
.我需要一个去抖动功能,因为每次调整窗口大小时我都会做一些DOM交互,而且我不想让浏览器超载.典型的去抖功能只会在每个间隔调用一次传递函数; 间隔通常是第二个参数.我假设对于大量的UI工作,最佳间隔是不会使浏览器过载的最短时间.在我看来,这正是requestAnimationFrame
将要做的:
var debounce = function (func, execAsap) {
var timeout;
return function debounced () {
var obj = this, args = arguments;
function delayed () {
if (!execAsap)
func.apply(obj, args);
timeout = null;
};
if (timeout)
cancelAnimationFrame(timeout);
else if (execAsap)
func.apply(obj, args);
timeout = requestAnimationFrame(delayed);
};
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是从上面的debounce
链接直接剽窃,但使用requestAnimationFrame而不是setTimeout.根据我的理解,这将尽快排队传入函数,但任何比浏览器更快处理的调用都将被删除.这应该产生最平滑的交互.我是在正确的轨道上吗?还是我误会了requestAnimationFrame
?
(当然这仅适用于现代浏览器,但是requestAnimationFrame有简单的polyfill,它们可以回退到setTimeout.)
我有一个博客资源的blogs_controller,所以我现在有了你的典型路线如下:
/blogs/new
/blogs/1
/blogs/1/edit #etc
Run Code Online (Sandbox Code Playgroud)
但这就是我想要的:
/blogs/new
/blogs/2010/01/08/1-to_param-or-something
/blogs/2010/01/08/1-to_param-or-something/edit #etc
...
/blogs/2010/01 # all posts for January 2010, but how to specify custom action?
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过map.resources和map.connect的组合来做到这一点,但我有很多通过"new_blog_path"等链接到其他页面的视图,我不想去编辑那些.这可以单独使用map.resources了吗?这可能并不容易,但我并不反对聪明.我想的是:
map.resources :blogs, :path_prefix => ':year/:month/:day', :requirements => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/}
Run Code Online (Sandbox Code Playgroud)
但是我不确定它如何与"新"或"创建"等行为一起工作,它也给了我一条路径,就像/2010/01/08/blogs/1-to_param-etc
URL中间的博客一样.
那么,有一个我缺少的聪明解决方案,还是我需要去map.connect路由?