关联记录更改后重新加载模型

时间:2014-02-13 09:40:06

标签: ember.js ember-data

我有一个带有几个hasMany关联的嵌套列表:

Customers
    Projects
        Tasks

现在我使用loadData方法获取此数据:

var _this = this;

// get customers with active projects
this.store.find('customer', { hasProjects: true, getArchivedProjects: false }).then(function(customers) {

  // sort customers by name
  customers = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
    sortProperties: ["name"],
    content: customers
  });

  // build array which includes customers and their projects
  var promises = customers.map(function(customer) {

    return Ember.RSVP.hash({
      customer: customer,
      projects: customer.get('projects').then(function(projects) {

        // reload projects
        var project_array = [];
        projects.forEach(function(project) {
          project_array.push(project.get('id'));
        });

        projects = _this.store.findQuery('project', { ids: project_array, archived: false });

        // sort projects by name
        return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
          sortProperties: ["name"],
          content: projects
        });

      })
    });

  });

  Ember.RSVP.all(promises).then(function(filteredProjects) {
    _this.set('filteredProjectsWithoutUnassignedOnes', filteredProjects);
    _this.getUnassignedProjects(false);
  });

}

问题是:向项目添加任务时,模板不会更新,因为项目模型不会更新(请参阅此行:projects: customer.get('projects')

要更新模板,我写了这个脏代码:

// reload projects
var project_array = [];
projects.forEach(function(project) {
  project_array.push(project.get('id'));
});

projects = _this.store.findQuery('project', { ids: project_array, archived: false });

还有其他方法可以实现吗?我已经阅读了很多关于观察者的内容,但我不知道,在这个特定情况下将观察者放在哪里。

目标是在向project添加tasks时更新{{1}}模型。

0 个答案:

没有答案