Ember Data仅在渲染到Handlebars Template时加载Async属性

时间:2014-09-26 03:06:38

标签: asynchronous ember.js ember-data

我希望Ember只在需要时加载相关数据并将其呈现给模板。我认为将async设置为true会这样做,但它只是在呈现父对象时异步加载数据。或者,我尝试仅在发生操作时加载它。

JS代码在这里(非工作,但显示一般的想法): http://jsbin.com/tekaju/2/edit

对于那些不关心检查的人,要点是:

Models.Visit = DS.Model.extend({
 visitors: DS.hasMany("user", { async: true })
});


VdAdmin.VisitController = Ember.ObjectController.extend({
  showVisitors: false,
  showVisitorsObserver: function(value){
    showVisitors = value;
    return value;
  }.observes('showVisitors')
  actions: {
    loadVisitors: function(){
      this.set('showVisitorsObserver', true);
    }
  }
});

<button ... {{action 'loadVisitors'}}>Click to Load</button>
{{#if showVisitorsObserver }}
   I don't want to asynchronously grab related `visitors` data until `showVisitors` is true
   {{visitors.length}}
{{/if}}

1 个答案:

答案 0 :(得分:1)

你不应该看一个观察者,它不会返回值。它用于反应,而不是作为财产。您应该注意showVisitors

App.ItemController = Em.ObjectController.extend({
  showVisitors:false,
  actions: {
    loadVisitors: function(){
      this.set('showVisitors', true);
    }
  }
});

{{#if showVisitors }}
   I don't want to asynchronously grab related `visitors` data until `showVisitors` is true
   {{visitors.length}}
{{/if}}

示例:http://emberjs.jsbin.com/qabaf/4/edit