1

在修订版 12 之前,我将简单地执行以下操作来创建一个对象,并且它与“hasMany”关系相关联

  test("findMany generates http ...", function() {
    store.load(Person, {id: 9, name: "Toran Billups", tasks: [1, 2]});
    person = store.find(Person, 9);
    expectLoaded(person);

    equal(ajaxUrl, undefined, "no Ajax calls have been made yet");

    var tasks = get(person, 'tasks');

    equal(get(tasks, 'length'), 2, ""); //this now fails showing 0 tasks ...
  });

但是在升级之后,“任务”hasMany 似乎是 0 而不是 2,正如我所预料的那样。

如果有任何帮助,这里是模型

  var attr = DS.attr, hasMany = DS.hasMany, belongsTo = DS.belongsTo;

  Person = DS.Model.extend({
    name: attr('string')
  });

  Task = DS.Model.extend({
    name: attr('string'),
    isFinished: attr('boolean'),
    owner: belongsTo(Person)
  });

  Person.reopen({
    tasks: hasMany(Task)
  });

我知道在加载几个任务后我可以做一个简单的“pushObject”,但我上面测试的目的是验证 $.ajax 行为,当我只有关联的任务 ID 并且 ember-data 会获取模型的其余部分(延迟加载)。是否可以保留它,就像我在第 12 版中所拥有的那样?

4

1 回答 1

1

看起来您需要在修订版 12 中使用“loadHasMany”选项 - 按预期工作!

  test("findMany generates http ...", function() {
    store.load(Person, {id: 9, name: "Toran Billups"});
    person = store.find(Person, 9);
    store.loadHasMany(person, 'tasks', [ 1, 2 ]);
    expectLoaded(person);

    equal(ajaxUrl, undefined, "no Ajax calls have been made yet");

    var tasks = get(person, 'tasks');

    equal(get(tasks, 'length'), 2, ""); //this actually returns 2 :)
  });
于 2013-04-01T00:46:37.720 回答