AngularJS中的同步$资源(Synchronous $resource in AngularJS)
我处于死胡同,因为我知道Javascript不是用于同步工作,特别是AngularJS。 但是,我遇到了困难的情况。
我在域名“www”(使用AngularJS)上有一个主页,它调用了很多“api”。 “api”上的一些资源需要身份验证,因此它返回401,然后AngularJS会在它看到时选择并显示一个登录框。
www-login支持一堆登录方法,并将密码/ oauth / whatever发送到“api”,后者返回角度应用程序存储在cookie中的令牌,并设置为
$http.defaults.headers.common['Authorization']
因此它可用于验证对api的进一步请求。同样的方法也用于获取属于api-token的用户名。
这完美地工作并解决了我的设计目标。 但是如果浏览器要访问需要身份验证的URL,则会弹出401(因此弹出登录框)。 这是(我猜)因为在401访问浏览器之前,angular无法填充标题中的Authorization字段。
解决方案?
- 使用jquery有一个async = false请求?
- 完成获取所需数据后,关闭登录框。 登录框可能会闪烁......
- 在cookie中存储更多关于登录(即用户名)的元数据,因此我们无需在加载应用程序时从服务器获取此信息。
- ??
有更好的解决方案吗? 这一次,对于这一个请求,我想在我的Angular资源中使用async = false ....
I'm in a dead end, because I know that Javascript isn't made for synchronous work, and specially not AngularJS. However, I am in a situation where I am stuck needing it.
I have one main page on the domain "www" (with AngularJS), that calls "api" a lot. Some of the resources on "api" requires authentication, so it returns an 401, which in turn AngularJS picks up and displays a login-box when it sees.
The www-login supports a bunch of login methods and sends the password/oauth/whatever to the "api", which returns a token which the angular app stores in a cookie, and sets in
$http.defaults.headers.common['Authorization']
so it can be used to authenticate furter requests to the api.The same method is also used to get the username that belongs to the api-token.
This works perfectly and solves my design goals. But if the browser is going to an url that requires authentication, the 401 (and hence the login box pops-up). This is (I guess) because angular is not able to populate the Authorization field in the header before the 401 hits the browser..
Solutions?
- Have an async=false request using jquery?
- Close the login-box when we are done getting the data we want. The login box might flicker...
- Store more meta-data about the login (ie, username) in cookies, so we dont haveto do get this information from the server when the app is loading.
- ??
Is there a better solution? This one time, for this one request, I want async=false in my Angular resource....
原文:https://stackoverflow.com/questions/18452176
最满意答案
最终,旧的内存将被释放,但你无法预测何时会发生这种情况。 它取决于Python实现和许多其他因素。
也就是说,对于您给出的示例和CPython实现,旧的数组应该在赋值期间进行垃圾回收。
(请注意,NumPy数组是讨论垃圾收集器行为的一个特别复杂的示例。)
Eventually, the old memory will be freed, though you cannot predict when this will happen. It is dependent on the Python implementation and many other factors.
That said, for the example you gave and the CPython implementation, the old array should be garbage collected during the assignment.
(Note that NumPy arrays are a particularly complex example for discussing garbage-collector behaviour.)
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
分配给null的变量和未分配的变量之间有什么区别(What is the difference between a variable assigned to null and others not assigned)[2022-06-25]
分配给null的变量与未分配的变量之间有什么区别? 一个未初始化的变量没有值,甚至不是null (这就是为什么它不能被读取) 初始化变量确实有一个值(例如null , 5或"hello" )。 请注意,成员变量会自动获取分配给它们的默认值(并且引用类型的默认值为null )。 所以虽然看起来你可以看到一个“未初始化”的成员变量,但事实上它已经被初始化了。 What is the difference between a variable assigned to null and others not ass ... -
如果一个函数在一种情况下返回一个字符串文字,而在另一种情况下返回一个动态全部内存的指针,这是一个坏主意。 您的函数应始终返回动态分配的内存或NULL 。 所以你可以free功能之外的内存。 const char* lookup( const char* path ) { char* rawEnding = strrchr( path, '.' ); if ( rawEnding == NULL ) return NULL; char* ending = rawEnd ...
-
NSTimer的内存管理 - 是否需要将其分配给变量?(Memory management of NSTimer — does it need to be assigned to a variable?)[2023-05-26]
在任何一个片段中都没有内存问题; 您只需要选择与计时器交互的需求。 当你计划一个计时器时,运行循环保留它,并且当它触发时传递对计时器的引用并且它的选择器 - 例如, (void) doTimerThing: (NSTimer *)tim 1 - 被使用,所以它不是你必须把它变成一个变量并自己保留它。 Apple在Timer Programming Topics文档的“Memory Management”部分清楚地解释了这一点。 但是,如果您可能希望在计时器触发之前 (或者在重复计时器的触发之间)使计时器无效 ... -
我尝试了下面的剪辑,它的工作原理。 码: word = "PythonWizard" print word begin = int(raw_input("Begin:")) if begin: begin = int(begin) end = int(raw_input("End:")) print "word[", begin,":",end,"] ", print word[begin:end] raw_input("\n\nPress the Enter Key to ...
-
为保存文件内容而创建的临时字符串将被销毁。 不需要深入研究源代码就可以确认,这里有几种方法可以测试作为函数参数创建的临时值被销毁: 方法1:报告其破坏的类 这通过使用报告其自身消亡的课程来展示生命期: class lifetime { public function __construct() { echo "construct\n"; } public function __destruct() { echo "destruct\ ...
-
Python:当为变量分配新内容时,是“旧的”内存是免费的吗?(Python: is the “old” memory free'd when a variable is assigned new content?)[2023-10-18]
最终,旧的内存将被释放,但你无法预测何时会发生这种情况。 它取决于Python实现和许多其他因素。 也就是说,对于您给出的示例和CPython实现,旧的数组应该在赋值期间进行垃圾回收。 (请注意,NumPy数组是讨论垃圾收集器行为的一个特别复杂的示例。) Eventually, the old memory will be freed, though you cannot predict when this will happen. It is dependent on the Python impleme ... -
一旦函数完成,Python实际上就释放了内存。 这里的问题是你打印出来的值, resource.getrusage(resource.RUSAGE_SELF).ru_maxrss ,告诉你峰值或最大内存使用量。 要获取当前的内存使用量,您可以尝试使用psutil包( $ pip install psutil )。 它是一个跨平台实用程序,可以为您提供当前内存使用情况等信息。 试试这个修改过的片段: import numpy as np import resource import os import psu ...
-
如果您查看有关V8垃圾收集的这篇文章,您将看到: 大对象空间:此空间包含的对象大于其他空间的大小限制。 每个对象都有自己的mmap'd内存区域。 垃圾收集器永远不会移动大对象。 我不知道什么是“大型物体”的具体细节,但是一个1000万的数组似乎可能符合描述。 问题是,如果垃圾收集器永远不会移动大对象,那么它们是如何清理的? 我还没有找到答案。 但是,如果在终端中启动节点,然后重复运行此命令 var myarr = new Array(1e7) 你会看到内存确实会增加,但只会到某一点。 我发现只要内存使用超 ...