Toran Billups
Toran Billups

Reputation: 27399

How to setup QUnit data for ember-data revision 12

Before revision 12 I would simply do the following to create an object and it's associated "hasMany" relationship

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

But after the upgrade the "tasks" hasMany seems to be 0 instead of 2 as I would have expected.

Here are the models if that helps in any way

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

I know I could do a simple "pushObject" after loading in a few tasks but the purpose of my test above was to verify the $.ajax behavior when I only had the task ids associated and ember-data would fetch the rest of the model (lazy loading). Is it possible to keep it as I had it above in rev 12 ?

Upvotes: 1

Views: 311

Answers (1)

Toran Billups
Toran Billups

Reputation: 27399

Looks like you need to use the "loadHasMany" option in revision 12 -works as expected!

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

Upvotes: 1

Related Questions