如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)

我正在使用Ember.js中的表单,并且想要检索所有模型属性的列表,以便我可以在不同时刻拍摄表单状态的快照。 有没有办法获得模型的所有属性列表?

例如,如果我的模型是:

App.User = DS.Model.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  current_password: DS.attr('string'),
  password: DS.attr('string'),
  password_confirmation: DS.attr('string'),
  admin: DS.attr('boolean'),
}

然后我想要这样的东西:

> getEmberProps('User')

["name", "email", "current_password", "password", "password_confirmation", "admin"]

I'm working with forms in Ember.js and I want to retrieve a list of all model properties so that I can take snapshots of the state of the form at different moments. Is there a way to get a list of all properties of a model?

For example, if my model is:

App.User = DS.Model.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  current_password: DS.attr('string'),
  password: DS.attr('string'),
  password_confirmation: DS.attr('string'),
  admin: DS.attr('boolean'),
}

Then I would like to have something like this:

> getEmberProps('User')

["name", "email", "current_password", "password", "password_confirmation", "admin"]

原文:https://stackoverflow.com/questions/15658442
2024-05-08 20:05

满意答案

内存页面在您使用它们之前并未实际映射到您的程序。 所有的malloc都会保留一个虚拟地址空间的范围。 在您尝试读取或写入物理RAM之前,没有将物理RAM映射到这些虚拟页面。

即使您分配全局或堆栈(“自动”)内存,在您触摸它们之前也没有物理页面的映射。

最后, sizeof()是在编译时计算的,当编译器不知道OS将在以后做什么时。 所以它只会告诉你对象的预期大小。

如果您尝试在每种情况下将内存memset为0,您会发现事情会表现得非常不同。 另外,你可能想尝试一下calloc ,它将它的内存归零。


Memory pages aren't actually mapped to your program until you use them. All malloc does is reserve a range of the virtual address space. No physical RAM is mapped to those virtual pages until you try to read or write them.

Even when you allocate global or stack ("automatic") memory, there's no mapping of physical pages until you touch them.

Finally, sizeof() is evaluated at compile time, when the compiler has no idea what the OS will do later. So it will just tell you the expected size of the object.

You'll find that things will behave very differently if you try to memset the memory to 0 in each of your cases. Also, you might want to try calloc, which zeroes its memory.

相关问答

更多

在c ++中可以动态分配的最大内存和编译时间(Maximum memory that can be allocated dynamically and at compile time in c++)

内存页面在您使用它们之前并未实际映射到您的程序。 所有的malloc都会保留一个虚拟地址空间的范围。 在您尝试读取或写入物理RAM之前,没有将物理RAM映射到这些虚拟页面。 即使您分配全局或堆栈(“自动”)内存,在您触摸它们之前也没有物理页面的映射。 最后, sizeof()是在编译时计算的,当编译器不知道OS将在以后做什么时。 所以它只会告诉你对象的预期大小。 如果您尝试在每种情况下将内存memset为0,您会发现事情会表现得非常不同。 另外,你可能想尝试一下calloc ,它将它的内存归零。 ...

确定C中动态分配的内存大小(Determine size of dynamically allocated memory in C)

comp.lang.c常见问题列表·问题7.27 - 问:所以我可以查询malloc包,找出分配块的大小? 不幸的是,没有标准或便携式的方式。 (有些编译器提供非标准扩展名。)如果您需要知道,您必须自己跟踪它。 (另见问题7.28 ) comp.lang.c FAQ list · Question 7.27 - Q. So can I query the malloc package to find out how big an allocated block is? A. Unfortunate...

“编译时分配的内存”是什么意思?(What does “Memory allocated at compile time” really mean?)

在编译时分配的内存意味着编译器在编译时解析某些事情将在进程内存映射中分配。 例如,考虑一个全局数组: int array[100]; 编译器在编译时知道数组的大小和int的大小,所以它在编译时知道数组的整个大小。 默认情况下,全局变量还具有静态存储持续时间:它被分配在进程内存空间(.data / .bss部分)的静态内存区域中。 鉴于这些信息, 编译器在编译期间决定该阵列的静态内存区域的地址 。 当然,内存地址是虚拟地址。 程序假设它有自己的整个内存空间(例如从0x00000000到0xFFFF...

使用动态分配和静态分配的共享内存(Using both dynamically-allocated and statically-allocated shared memory)

共享内存分为两部分:静态分配和动态分配。 第一部分是在编译期间计算的,每个声明都是一个实际的分配 - 在编译期间激活ptxas信息说明了它: ptxas info : Used 22 registers, 384 bytes smem, 48 bytes cmem[0] 这里,我们有384个字节,这是3 32整数的数组。 (见下面的样本corde)。 您可以将指向共享内存的指针从Kepler传递到另一个允许设备子功能访问另一个共享内存声明的函数。 然后,动态分配共享内存,在内核调用期间...

C / C ++:动态分配内存的按位运算符(C/C++: Bitwise operators on dynamically allocated memory)

正如John Knoeller建议的那样,我假设你想要从一个字节到下一个字节的位数。 这里的要求不足。 您需要指定相对于字节顺序的位的顺序 - 当最低有效位超出一个字节时,将转到下一个较高字节或下一个较低字节。 但是,您所描述的内容在图形编程中非常常见。 您基本上已经描述了单色位图水平滚动算法。 假设“正确”意味着更高的地址但更低的有效位(即匹配两者的正常写入约定),单位移位将类似于...... void scroll_right (unsigned char* p_Array, int p_Si...

在C ++中释放动态分配内存的问题(Issue With Freeing Dynamically Allocated Memory In C++)

你从未分配任何内存。 int numSurveyed = 0; //Pointers int * movieData_ptr = nullptr; movieData_ptr = new int[numSurveyed]; 这相当于 int *movieData_ptr = new int[0]; 您正在分配0个整数的大小。 这是未定义的行为。 没有分段错误,您无法使用该指针进行任何有用的操作。 您需要预先分配一定数量,并确保不会溢出,或者每次计划添加数据时都要动态分配。 既然这是C ++,...

在C中动态分配内存的互斥量(Mutex for dynamically allocated memory in C)

答:我认为在“全局列表”这个术语下,作者理解了线程之间共享的所有变量。 例: struct foo* shared_foo; /* This pointer is shared between all threads */ struct foo* foo_alloc(void) { /* This pointer is local to the thread which allocates the memory */ struct foo *fp; if ((fp = ma...

在C ++中对动态分配的内存进行碎片整理(Defragmentation of dynamically allocated memory in C++)

C ++堆中没有碎片整理,因为应用程序可以自由地保持指向已分配内存的指针。 因此,堆管理器无法移动已分配的内存。 唯一可能的“碎片整理”是你释放两个相邻的块。 然后堆管理器将两个块组合成一个更大的空闲块,可以再次用于分配。 There is no defragmentation in the C++ heap because the application is free to keep pointers to allocated memory. Thus the heap manager can...

MPI_Abort和动态分配的内存(MPI_Abort and dynamically allocated memory)

MPI_Abort()类似于exit() : 这个例程是“最好的尝试”,以中止comm中的所有任务。 此函数不要求调用环境对错误代码执行任何操作。 但是,Unix或POSIX环境应该将其作为主程序的返回错误代码来处理。 ( MPI 3.1规范 ) 在中止进程有效的情况下,由这些进程保存的任何动态分配的内存都将释放回操作系统。 动态分配不会在进程终止后继续存在。 所以, 不释放动态分配的内存会导致故障/不可预测的行为? 在某种程度上。 这被认为是漏洞吗? 由MPI_Abort()的操作引起的mall...

删除动态分配的内存(Deletion of Dynamically Allocated Memory)

string_copy的调用者负责通过调用delete[]来释放内存。 顺便说一句,这是编写C ++代码的一种可怕方式。 你应该使用std::string或std::vector<char>或类似的东西。 原因如下: int length = string_length(string_c); char* copy = string_copy(string_c); cout << "Copied String: " << copy << endl; delete[] copy; return 0; ...

相关文章

更多

rails model的一点疑惑。。。。。

在rails中生成的model里面定义的属性都直接对应数据库字段,如何定义不需要对应数据库的属性呢? ...

Java:IO/NIO篇,读写属性文件(properties)

1. 描述 尝试用多种方法读取属性文件。 测试打印系统属性; 测试读取、写入用户属性文件; 测试 ...

log4j.properties被自动删除

我把log4j.properties文件放到&quot;WEB-INF\classes&quot;目录 ...

solrcore.properties定义server是否是master

If the configuration directory for a Solr core cont ...

页面获取ACTION的属性,页面不能弹出JS

action定义一个属性,get set后 对属性赋值,值是JS,页面用&lt;s:property/ ...

Hibernate中如何实现一个model和多个表关联

RT, 如何能不创建额外的对象的前提下,把一个对象拆到多个表中? 问题补充: 示例model p ...

Backbone.js的技巧和模式

本文由白牙根据Phillip Whisenhunt的《Backbone.js Tips And Pat ...

Cannot expose request attribute 'website' because of an existing model object of the same name 的解决方案

在使用springmvc+freemarker,有可能你会遇到以下异常(如:资源找不到重定向到404的 ...

ext 中 ajax怎么异步加载js文件 ?

ext 中 ajax怎么异步加载js文件 ? 谁能提供下代码? 问题补充: 想找个一好用的的 我找 ...

微信XML消息model定义之微信公众平台(一)

很简单的model定义。 packagecn.wx.server;importorg.dom4j.Do ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本