有没有办法将Ember Object转换为纯粹的JavaScript对象? [英] Is there any way to convert Ember Object into plain javascript object?

查看:121
本文介绍了有没有办法将Ember Object转换为纯粹的JavaScript对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何方式来完成这种转换的任务,因为我找不到任何获取对象的Ember.js属性的方法。 Ember.keys 仅返回在创建 get Ember.extend 中声明的属性不会显示在那里。我使用这样的属性来设置默认值(例如,数组属性的 []

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)

推荐答案

目前我用以下代码片段解决了这个问题:

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;
  }
});

它不会上升类层次结构,不会调查mixins(因为我用于数据对象这是非常简单的形式)

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)

这篇关于有没有办法将Ember Object转换为纯粹的JavaScript对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆