Is there any way to convert Ember Object into plain javascript object?

夙愿已清 提交于 2019-12-19 05:17:15

问题


I could not find any way to accomplish the task of such conversion as I could not find any means of getting Ember.js properties for the object. Ember.keys returns only the properties I set in create or with get and the properties declared in Ember.extend do not show up there. I use such properties to set up default values (e.g. [] for array properties)


回答1:


Here is my dirty workaround

var newModel = JSON.parse(JSON.stringify(model));



回答2:


I would do something similar to the person above, but I'd do it a little bit differently.

Mixin

App.NativeObject = Ember.Mixin.create({
    toNative: function() {
        var properties = [];
        for (var key in this) {
            if (jQuery.inArray(Ember.typeOf(object[key]), ['string', 'number', 'boolean']) !== -1) {
                properties.push(key);
            }
        }
        return this.getProperties(properties);
    }
});

Object

Then you just need to implement the App.NativeObject mixin in your objects that you would like the toNative on:

var Object = Ember.Object.extend(App.NativeObject, {
    name: 'Adam',
    count: 4
});

We then have the toNative method on all the objects that implement our mixin.

Obligatory jsFiddle: http://jsfiddle.net/jumUx/




回答3:


If your object is a subclass of ember-data model notice you can use the toJSON method otherwise you can use:

JSON.parse(JSON.stringify(emberObj))

To grab any values which support native json serialization (i.e. not functions/methods)




回答4:


This worked for me:

myModel.toJSON({includeId: true})

I'm using Ember 3.




回答5:


This is what I did and it works quite well. Note, this should be ready only, as any changes to an object or array in the copied object will affect the original object

App.BaseValidations = Ember.Object.create({
    toObject: function() {
        var destination = {}
        for (var k in this) {
            if (this.hasOwnProperty(k) && typeof(this[k]) !== 'function') {
                destination[k] = this[k];
            }
        }
        return destination;
    }
})



回答6:


something quite simple that worked properly enough for me is :

Ember.Object.reopen({
    toJson: function() {
        return JSON.parse(JSON.stringify(this));
    }
});

at app loading time.




回答7:


At the moment I solve it with the following snippet:

App.plainCopy = function (obj) {
  if (Ember.isArray(obj)) {
    return obj.map(App.plainCopy);
  } else if (typeof(obj) === "object") {
    if (App.Plainable.detect(obj)) {
      return obj.plainCopy();
    } else {
      throw new Error(Ember.String.fmt("%@ is not Plainable", [obj]));
    }
  } else {
    return obj;
  }
}

App.Plainable = Ember.Mixin.create({
  plainCopy: function() {
    var props = Ember.keys(this);
    var proto = this.constructor.prototype;
    for(p in proto) {
      if (proto.hasOwnProperty(p) && typeof(this[p])!=="function") {
        props.push(p);
      }
    }
    var copy = {};
    props.forEach(function(p) {
      copy[p] = App.plainCopy(this.get(p));
    }, this);
    return copy;
  }
});

It does not go up the class hierarchy and does not look into mixins (as I use for data objects which are quite simple form that point of view)




回答8:


Another possible solution that may suit your needs while not being fully recursive for nested Ember objects:

// where myEmberObject is.. an ember object
var plainJavaScriptObject = myEmberObject.toJSON();

This will only include actual properties that you've defined and no Ember internals. Again, the drawback here is that any nested Ember objects will not, themselves, be converted but will appear as Strings in style of "".



来源:https://stackoverflow.com/questions/15001409/is-there-any-way-to-convert-ember-object-into-plain-javascript-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!