Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ember中嵌入数据的处理_Javascript_Ember.js - Fatal编程技术网

Javascript ember中嵌入数据的处理

Javascript ember中嵌入数据的处理,javascript,ember.js,Javascript,Ember.js,假设我有一个图(树,随便什么),在纯JS中的结构如下: var trees = [{ name : 'my tree', nodes : [ { id : 0, name : 'node 0', type : 'fruit', connectedTo : [1,2] },{ id : 1, name : 'node 1', type : 'fruit', connectedTo

假设我有一个图(树,随便什么),在纯JS中的结构如下:

var trees = [{
  name : 'my tree',
  nodes : [
    {
      id : 0,
      name : 'node 0',
      type : 'fruit',
      connectedTo : [1,2]
    },{
      id : 1,
      name : 'node 1',
      type : 'fruit',
      connectedTo : [3,4]
    },{
      id : 2,
      name : 'node 2',
      type : 'fruit',
      connectedTo : []
    },{
      id : 3,
      name : 'node 3',
      type : 'non-fruit',
      connectedTo : []
    },{
      id : 4,
      name : 'node 4',
      type : 'non-fruit',
      connectedTo : []
    }
  ]
}]
现在在Ember,它需要我有一个
节点
,可能还有一个类型模型。没什么大不了的。但是,我需要为树形图解析数据。但是当我访问数据时,它就像一系列
get()
then()
调用(模型之间的关系是异步的)


我希望用点表示法访问它们,类似于
tree.nodes[1]。键入
。在余烬有可能吗?我读过几篇关于嵌入式数据的文章,他们所做的只是将外部数据规范化为Ember模型。我不想那样。我想要的是处理应用程序内部的嵌入式数据。

您可以在Ember中使用plainJS对象,而不需要像下面这样使用Ember数据


我注意到
firstObject
。有没有办法将其设置为任意值?这是一种奇怪的表示法。发现我仍然可以本地访问它们,只需使用
get
set
触发观察者。这是一个很大的帮助,虽然,从来都不知道这是可能的。再次感谢!:D
this.get('nodes').then(function(nodes){
  nodes.forEach(function(node){
    node.get('type').then(function(type){
      console.log(type.get('type')) // OMG
    });
  })
});
<script type="text/x-handlebars" data-template-name="index">
    <ul>
    {{model.name}}
    {{#each item in model.nodes}}
      <li>{{item.id}}</li>
      {{#each connectedTo in item.connectedTo}}
        {{connectedTo}}
      {{/each}}
    {{/each}}
    </ul>
</script>
App.IndexRoute = Ember.Route.extend({
  actions: {
    changeValues: function(){

      this.controller.set('content.name', 'changed');
      this.controller.set('content.nodes.firstObject.id', 7);


    }
  },
  model: function() {
    return {
  name : 'my tree',
  nodes : [
    {
      id : 0,
      name : 'node 0',
      type : 'fruit',
      connectedTo : [1,2]
    },{
      id : 1,
      name : 'node 1',
      type : 'fruit',
      connectedTo : [3,4]
    },{
      id : 2,
      name : 'node 2',
      type : 'fruit',
      connectedTo : []
    },{
      id : 3,
      name : 'node 3',
      type : 'non-fruit',
      connectedTo : []
    },{
      id : 4,
      name : 'node 4',
      type : 'non-fruit',
      connectedTo : []
    }
  ]
};

  }
});