Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a reason there's no public API to convert an Ember.Object back to a native JS object #10875

Closed
atomkirk opened this issue Apr 13, 2015 · 10 comments

Comments

@atomkirk
Copy link

I find myself needing this a lot and I'm wondering if there is rationale behind why this hasn't been added to the framework.

I would think the use cases would be plentiful and obvious. An Ember.Object adds a LOT of metadata and extra properties to a plain object, so if you ever want to operate on an object with a generic function that iterates over an object's properties, those properties really get in the way.

@workmanw
Copy link

I guess it just depends on how you're using the framework. I personally have never found this need, the only time I can imagine wanting this would be for object serialization and I use ember-data, so that need is not relevant.

That said, if I did have this problem, I would start by using Ember.ObjectProxy and see how far that got me. Here is an example: http://emberjs.jsbin.com/kapenomivu/1/edit?js,output

@stefanpenner
Copy link
Member

generic function that iterates over an object's properties

this is what hasOwnProperty is for

feel free to use, Ember.keys which is more or less Object.keys

Ember.keys(Ember.Object.create({ foo: 1 }))
>  [ 'foo' ]

@stefanpenner
Copy link
Member

That said, if I did have this problem, I would start by using Ember.ObjectProxy and see how far that got me. Here is an example: http://emberjs.jsbin.com/kapenomivu/1/edit?js,output

I would likely recommend against this, why not just use the pojo directly?

@workmanw
Copy link

@stefanpenner I think that's really a question for the OP. I guess I was assuming that he had a class that had computed properties/functions/observers/etc. I had not considered just iterating with Ember.keys, I thought that would include stuff from the class (I swear way back in the day it did), but was pleasantly surprised to find it didn't.

Just out of curiosity, why would you recommend against using Ember.ObjectProxy?

Edit: Just to clarify, the Ember.keys solution is far cleaner and gets my vote. Was just curious if you had any negative thoughts specifically around ObjectProxy.

@atomkirk
Copy link
Author

There are a few strategies to extract the original "Hash" structure that was used to create the ember object (with or without modified values), my question was mostly about how often the general Ember.js community needs this. a .nativeCopy method would be really nice on Ember.Object but I appreciate the necessity to be extremely selective with API additions. Sometimes it makes sense to add a method that makes an extremely common operation more convenient and sometimes it doesn't. I get the feeling people do not run into this need as often as I do.

One concrete example, if you're curious, is using an Ember.Object to back a "Create New […]" form. Perhaps the create form is right below a list of items. If you use this.store.createRecord to create a blank model to back the form, it immediately adds a new item to the list above and fills out the values as you fill out the form. To avoid this I might create an Ember.Object to back the form, but then I have to get the values out one by one to pass to createRecord

One last reason I do this a lot is to create a deep copy since implementing Ember.Copyable is such a pain. I like the design of Ember.Copyable but a sane default/generic implementation would be really really nice.

@ccarterc
Copy link

ccarterc commented Aug 1, 2016

The biggest reason for needing pojo's is for 3rd party library support. Datatables, jstree, etc. all expect plain javascript arrays and objects and will blow up with ember data structures.

@SlyDen
Copy link

SlyDen commented Aug 9, 2017

@ccarterc like, e.g. localforage.setItem - it is PITA with Ember.Object
Faced with it today and had to put custom "escaper" for that.

@jamesdixon
Copy link

Ran into this exact need today. As @ccarterc said, this is crucial for 3rd party libs that are expecting POJOs. It would be great to have an "official" method to do this.

@majew7
Copy link

majew7 commented Jan 9, 2018

I want the ability to work with a POJO, so I can send my object into a 3rd party vanilla JS library.

@teddycoleman
Copy link

yes please - struggling with this now for a 3rd party api. my current workaround is to do getProperties for an object with an array of about 70 fields on it. this seems silly and having a function to do that would be great...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants