如何从模型类型中检索数据?

How to retrieve data from a model type?

我完全是 Ember 的菜鸟,我必须从用 Ember 编写的外部网站检索数据。

我安装了 Ember 检查器插件,我看到了以下内容:

控制台调试window可以输入什么javascript命令来获取列表中的数据(Id和Number)?

我不能依赖 Ember inspector 进行数据检索,该工具仅用于检查结构。


更新 1

感谢@kumkanillam,我做到了这里:

我似乎可以知道列表中某个项目的每个属性的名称和类型,但我无法获取它的值。

happy 数组是他的示例调用的结果:myStore.peekAll('common/following-info').toArray()

看看这个
https://guides.emberjs.com/v2.14.0/ember-inspector/container/ https://guides.emberjs.com/v2.14.0/ember-inspector/object-inspector/#toc_exposing-objects-to-the-console

图片解释的很好

您可以通过单击检查器中的 $E 按钮将对象公开到控制台。这会将全局 $E 变量设置为所选对象。

您还可以向控制台公开属性。当您将鼠标悬停在对象的属性上时,每个 属性 旁边都会出现一个 $E 按钮。单击它可将 属性 的值显示到控制台。


更新 1

您可以在控制台中运行下面的代码。

 function getApplication() {
  let namespaces = Ember.Namespace.NAMESPACES;
  let application;

  namespaces.forEach(namespace => {        
    if (namespace instanceof window.Ember.Application) {
      application = namespace;
      return false;
    }
  });
  return application;
}

您可以使用上述函数获取应用程序实例,从那里您可以查找访问 service:store,从那里您可以使用 peekAll 所需的模型。为了单独查看所有需要的数据,我使用了 JSON 方法,例如 stringify 然后解析。

我实际上使用了 LinkedIn 网站并使用他们的模型 common/following-info 进行演示。您可以选择您想要查看的模型,

var myApp = getApplication();
var myStore = myApp.__container__.lookup('service:store')
myStore.peekAll('common/following-info')
myStore.peekAll('common/following-info').toArray()
JSON.stringify(myStore.peekAll('common/following-info').toArray())
JSON.parse(JSON.stringify(myStore.peekAll('common/following-info').toArray()))

更新 2

myStore.peekAll('common/following-info')给你returnsDS.RecordArray and it extends Ember.ArrayProxy, that means you can use methods available in ArrayProxy. forEach to iterate or to get specific index record use objectAt(index)

在您的情况下,您需要知道模型的 属性 名称才能获取特定 属性 的值,例如

let allRecords = myStore.peekAll('common/following-info');
allRecords.forEach(function(item){
  console.log(item);
  console.log(' Using get method to value ', item.get('propName'));
});

获取特定的索引值,

let allRecords = myStore.peekAll('common/following-info');
let firstRecord = allRecords.objectAt(0);
console.log(' First record propName value is',firstRecord.get('propName'));

在你的情况下,你想打印整个对象而不给每个 属性 名称,那么没有内置的方法,我们需要使用 JSON.stringify 和 [=22= 做一些黑客攻击] 以获得您正在寻找的完美对象。然后您可以使用 Object.values 获取所有值。

let arrayOfObjectWithOnlyKeysAndValuesOfModel = JSON.parse(JSON.stringify(myStore.peekAll('common/following-info').toArray()));
arrayOfObjectWithOnlyKeysAndValuesOfModel.forEach(function(item){ 
 console.log('item',item);
 console.log(' item values alone ', Object.values(item));
});