在js文件中有一系列命名函数是不好的做法吗?(Is it bad practice to have a series of named functions in js files?)
我正在编写一个js文件,它在doc-ready部分中有很多jquery命令,然后在jquery命令中引用了一大堆跟随它的函数。 这是一个简短的例子,
//jquery on ready $(function() { //misc jquery commands $('#bzm a').click(function(event) { event.preventDefault(); }); $('.editable').not('video, img, textarea').click(function(event) { event.stopPropagation(); loadEditor($(this)); return false; }); }); //bunch of named functions referenced by jquery commands function loadEditor(node, link){ event.stopPropagation(); var value = node.text(); if (editObj){ commitEditor(editObj); } if (node.not('video, img, textarea')){ $('#textEdit') .copyCSS(node) .offset(node.offset()) .css('display', 'block') .val(node.text()); node.css('color', 'transparent'); } if(node.is('a') || node.parent().is('a') ){ $('#urlEdit').show(); $('#urlEdit').val(node.attr('href')); } else {$('#urlEdit').hide();} editObj = node; if (link){ $('#urlEdit').select(); }else{ $('#textEdit').select(); } }
我觉得我已经看到它说命名函数会导致js或类似的东西表现不佳......我能找到的最接近的例子就在这里 。 我只是想肯定地知道。
I am writing a js file that has a lot of jquery commands within the doc-ready part and then a whole swath of functions following it that are referenced in the jquery commands. Here's a short example,
//jquery on ready $(function() { //misc jquery commands $('#bzm a').click(function(event) { event.preventDefault(); }); $('.editable').not('video, img, textarea').click(function(event) { event.stopPropagation(); loadEditor($(this)); return false; }); }); //bunch of named functions referenced by jquery commands function loadEditor(node, link){ event.stopPropagation(); var value = node.text(); if (editObj){ commitEditor(editObj); } if (node.not('video, img, textarea')){ $('#textEdit') .copyCSS(node) .offset(node.offset()) .css('display', 'block') .val(node.text()); node.css('color', 'transparent'); } if(node.is('a') || node.parent().is('a') ){ $('#urlEdit').show(); $('#urlEdit').val(node.attr('href')); } else {$('#urlEdit').hide();} editObj = node; if (link){ $('#urlEdit').select(); }else{ $('#textEdit').select(); } }
I feel like I've seen it said that named functions will cause poor performance in js or something like that.. the closest example I can find is here. I'd just like to know for certain.
原文:https://stackoverflow.com/questions/7313280
最满意答案
你需要添加:
$(document).delegate("a", "click", function(evt) { var href = $(this).attr("href"); var protocol = this.protocol + "//"; if (href.slice(protocol.length) !== protocol && protocol !== 'javascript://' && href.substring(0, 1) !== '#') { evt.preventDefault(); Backbone.history.navigate(href, true); } });
最后2个字符串应该是:
app_router.navigate("/login", {trigger: true}); // <- should remove this string Backbone.history.start({pushState: true, root:"test/backbone/test.html"});
You need add:
$(document).delegate("a", "click", function(evt) { var href = $(this).attr("href"); var protocol = this.protocol + "//"; if (href.slice(protocol.length) !== protocol && protocol !== 'javascript://' && href.substring(0, 1) !== '#') { evt.preventDefault(); Backbone.history.navigate(href, true); } });
And last 2 strings should be:
app_router.navigate("/login", {trigger: true}); // <- should remove this string Backbone.history.start({pushState: true, root:"test/backbone/test.html"});
相关问答
更多-
Backbone.history.start的root选项是路径而不是资源。 还要确保你杀死你的路线中的前导/ ,所以"p/:id"而不是"/p/:id"这应该让你去。 The root option in Backbone.history.start is a path not a resource. Also make sure you kill the leading / in your routes, so "p/:id" instead of "/p/:id" This should get yo ...
-
我还没有尝试过,但使用node.js和backbone的这些想法可能会有所帮助: http://andyet.net/blog/2011/feb/15/re-using-backbonejs-models-on-the-server-with-node/ http://bennolan.com/2010/08/13/pushstate-and-nodejs.html 基本上唯一的办法是让你不要两次写同样的东西,就是让你的node.js服务器和前端客户端共享相同的路由/模型代码。 如果你在服务器端使用其他的东 ...
-
Backbone.js和pushState(Backbone.js and pushState)[2023-06-17]
这是Tim Branyen在他的样板中使用的模式: initializeRouter: function () { Backbone.history.start({ pushState: true }); $(document).on('click', 'a:not([data-bypass])', function (evt) { var href = $(this).attr('href'); var protocol = this.protocol + '//'; ... -
如果关闭pushState使其在不支持它的浏览器中工作,那么尝试使用主干PushState测试来抢先关闭它(此代码基于Backbone源中的_hasPushState变量): // Enable pushState for compatible browsers var enablePushState = true; // Disable for older browsers var pushState = !!(enablePushState && window.history && window. ...
-
我想出了一个解决我自己问题的方法,很有趣。 实现pushState的问题在于,对于任何工作路由,即使是默认的归属路由,也需要后端服务器来初始呈现页面,以便Backbone可以在检查路由后触发正确的JavaScript。 这意味着开发本地实例并使用file://协议导航到该实例将不起作用。 (这就是我在上面的问题中做错了)。 对于这个简单的用例,我编写了一个简单的ExpressJS服务器,它在遇到任何 通配符路由时提供index.jade视图(我删除了index.html),然后允许Backbone使用这个小 ...
-
AngularJS路由器如何用于通配符和pushstate?(How does the AngularJS router work for wildcards and pushstate?)[2022-10-16]
1)角度路由中没有正则表达式的当前支持 2)Angular自动处理pushstate如果它在浏览器中不可用则会自动回退到hash模式#只需使用下面的行 $locationProvider.html5Mode(false); $locationProvider.hashPrefix('!'); 1) There is not current support for regex in angular routing 2)Angular automatically handle pushstate if it ... -
很好地使用另一个建议的帖子中的答案,这是在index.html文件中做
。 这实际上使我的pushState中的子目录工作! 只是它被建议..但是作为回报它打破了我的IE,但我通过在我的INIT骨干中添加额外的代码来修复它 Backbone.history.start({ pushState: Modernizr.history, silent: true }); if(!Modernizr.history) { var rootLength = Backbone ... -
禁用Backbone.js哈希完全,但保持pushState在历史?(Disable Backbone.js hashes entirely, but keep pushState in History?)[2021-11-12]
没有内置的支持,如果你的应用很难在旧版浏览器中工作,那么你可能会修改你的Backbone版本。 您可以在DomReady函数的开头添加如下内容: if (window.history && window.history.pushState) { // main script } else { alert("Your browser doesn't support push state and I don't want backwards compatibility"); win ... -
Backbone pushState路由(Backbone pushState routes)[2022-09-22]
你需要添加: $(document).delegate("a", "click", function(evt) { var href = $(this).attr("href"); var protocol = this.protocol + "//"; if (href.slice(protocol.length) !== protocol && protocol !== 'javascript://' && href.substring(0, 1) !== '#') ... -
在这种情况下,您有多种选择: 您可以拥有一个处理与客户端相同路由的服务器,并返回相同的结果。 它很难实现,但它提供了一个很好的网址。 Github这样做了。 始终返回index.html并处理路由客户端。 (这有点丑陋,难以维护) 不要使用pushstate。 阿门。 In this situation you have multiple options: You can have a server that handles the same routes as the client does and re ...