0

我有以下对象结构,我需要获取特定的标题值,然后将其设置为新值。我知道一旦你进入一个嵌套对象,你就会失去 Ember 对象的 .get() 和 .set() 方法。尝试以标准 JavaScript 方式设置值导致错误提示必须使用 ember.set()。

当它观察到对 App.job.code 的更改但我得到断言失败时,这就是我想要发生的事情:您必须使用 Ember.set() 来访问此属性([object Object])

updateTitle: function(){
    App.jobs.jobProducts[0].allocations[0].allocationTitle = "test2";
}.observes("App.job.code")

如何最好地实现这一目标?谢谢

App.jobs = [
  {
    id: 0,
    jobTitle: "This is the only job",
    jobProducts: [
      {
        id: 0,
        productTitle: "Product 1",
        allocations:[
          {
            id: 0,
            allocationTitle: "Allocation 1",
            deliverys:[
              {
                id: 0,
                deliveryTitle: "Delivery 1"
              },
              {
                id: 1,
                deliveryTitle: "Delivery 2"
              }
            ]
          },
          {
            id: 1,
            allocationTitle: "Allocation 2",
            deliverys:[
              {
                id: 0,
                deliveryTitle: "Delivery 3"
              },
              {
                id: 1,
                deliveryTitle: "Delivery 4"
              }
            ]
          }
        ]
      },
      {
        id: 1,
        productTitle: "Product 2",
        allocations:[
          {
            id: 0,
            allocationTitle: "Allocation 3",
            deliverys:[
              {
                id: 0,
                deliveryTitle: "Delivery 5"
              },
             {
               id: 1,
               deliveryTitle: "Delivery 6"
             }
           ]
          },
          {
            id: 1,
            allocationTitle: "Allocation 4",
            deliverys:[
              {
                id: 0,
                deliveryTitle: "Delivery 7"
              },
              {
                id: 1,
                deliveryTitle: "Delivery 8"
              }
            ]
          }
        ]
      }
    ]
  }
];
4

1 回答 1

0

我认为以标准香草方式访问对象没有问题:http: //jsbin.com/odosoy/74/edit

尝试这样的事情,它对你有用吗:

console.log("Title before: "+App.jobs[0].jobProducts[0].allocations[0].deliverys[0].deliveryTitle);

App.jobs[0].jobProducts[0].allocations[0].deliverys[0].deliveryTitle = "FOO";

console.log("Title after: "+App.jobs[0].jobProducts[0].allocations[0].deliverys[0].deliveryTitle);

更新:

在考虑之后,恕我直言,这是最好的做法,比如你的用例应该如何完成,这样你以后可以使用.set().get()以及框架为你提供的所有其他功能:

用关系定义你的模型

App.Job = DS.Model.extend({
  jobTitle: DS.attr('string'),
  jobProducts: DS.hasMany('App.JobProduct')
});

App.JobProduct = DS.Model.extend({
  productTitle: DS.attr('string'),
  allocations: DS.hasMany('App.Allocation')
});

App.Allocation = DS.Model.extend({
  allocationTitle: DS.attr('string'),
  deliveries: DS.hasMany('App.Delivery')
});

App.Delivery = DS.Model.extend({
  deliveryTitle: DS.attr('string')
});

以及一些让它生效的固定装置:

App.Job.FIXTURES = [
  {
    id: 1,
    jobTitle: "This is the only job",
    jobProducts: [1,2]
  }
];

App.JobProduct.FIXTURES = [
  {
    id: 1,
    productTitle: "Product 1",
    allocations:[1,2]
  },
  {
    id: 2,
    productTitle: "Product 2",
    allocations:[3,4]
  }  
];

App.Allocation.FIXTURES = [/* shortened */];

App.Delivery.FIXTURES = [/* shortened */];

在这里查看完整的工作演示

希望能帮助到你。

于 2013-08-23T11:17:50.487 回答