【问题标题】:Ember Computed Property on Array数组上的 Ember 计算属性
【发布时间】:2017-04-11 00:46:52
【问题描述】:

我是 Ember 新手,如果我遗漏了一些明显的东西,请原谅我(我已经花时间谷歌搜索这个问题,但仍然找不到解决方案),但在我看来,Ember 计算属性不是在长度等数组属性上按文档/预期工作。

我正在尝试建立自己的队列:

// app/custom-objects/processing-queue-item.js
import Ember from 'ember';

export default Ember.Object.extend({
  payload: null,
  extraContext: null,
  processingState: 'pending', // pending, succeeded, failed
  processingOutcome: null,    // null for pending, result for      succeeded, error for failed

  toString() {
    return `{ProcessingQueueItem: processingState=${this.get('processingState')}, processingOutcome=${this.get('processingOutcome')}, extraContext=${this.get('extraContext')}, payload=${this.get('payload')}}`;
  }
});

// app/custom-objects/processing-queue.js
import Ember from 'ember';
import ProcessingQueueItem from './processing-queue-item';

export default Ember.Object.extend(Ember.Enumerable, {
  queueName: null,

init() {
  this.set('items', []);
  this.get('items');
  this.get('items.length');
  this.get('length'); // Force observation
},

/*
 * Public API
 */

enqueue(payload, extraContext = null) {
 let itemToEnqueue = ProcessingQueueItem.create({ payload: payload, extraContext: extraContext });

 this.get('items').pushObject(itemToEnqueue);
 this.scheduleProcessing();

 return itemToEnqueue;
},

toString() {
  return `{ProcessingQueue: queueName=${this.get('queueName')}, length=${this.get('length')}}`;
},

 /*
  * Internal API
  */

scheduleProcessing() {
  Ember.run(() => {
    this.maybeProcessAnItem();
  });
},

maybeProcessAnItem() {
  console.log(`maybe process an item ${this}`);
},

/*
 * Ember.Enumerable mixin
 */

length: Ember.computed('items.length', function() {
  return this.get('items.length');
}),

nextObject(index, previousObject, context) {
  return this.get('items').nextObject(index, previousObject, context);
}
});

这个类不完整,但我想开始在模板中显示队列内容以帮助调试,但我无法让它工作。这是我的控制器和模板:

// app/controllers/dashboard.js
import Ember from 'ember';
import ProcessingQueue from '../custom-objects/processing-queue';

export default Ember.Controller.extend({
  init() {
  this._super(...arguments);
  this.set('processingQueue', ProcessingQueue.create({ queueName: 'DashboardQueue' }));
  this.get('processingQueue');
  this.get('processingQueue.length');
  this.get('queueLength');
 },

 queueLength: Ember.computed('processingQueue.length', function() {
   return this.get('processingQueue.length');
 }),
});

// app/templates/dashboard.hbs
<h1>Dashboard</h1>

<h2>Queue Length: '{{queueLength}}'</h2>
{{#each processingQueue as |queueItem|}}
<p>{{queueItem.payload}}</p>
{{/each}}

{{outlet}}

问题是,在&lt;h2&gt;Queue Length: '{{queueLength}}'&lt;/h2&gt; 中,队列长度始终未定义,直到我将项目添加到队列中。但这不是真的,队列有一个空数组,长度为 0。使用 EmberInspector 的仪表板控制器中的$E,我可以看到$E.get('processingQueue.length')$E.get('queueLength') 都是undefined

奇怪的是,一旦我将项目添加到队列中,队列长度就会被定义,1, 2, 3, ... 并在我添加队列项目时保持并同步模板。所以第一个$E.get('processingQueue').enqueue('foo') 会自动更新模板以显示队列长度为“0”,然后是“1”,依此类推。

为什么在我将任何项目排入队列之前它是未定义的?我尝试根据Unconsumed Computed Properties Do No Trigger Observers 在所有地方添加gets,但这似乎没有帮助。

有什么想法吗?我完全有可能在这里误解了关于计算属性的一些东西,但我不明白是什么以及为什么......我已经尝试过volatile(), [], @each 以及所有这些,我也无法让它有所作为。有点不对劲……

任何帮助都将不胜感激,我愿意添加到 Wiki 中,写一篇博文,并可能将我的队列作为开源发布以表示感谢。 :-)

谢谢!再次感谢您让 Ember 变得如此出色!

【问题讨论】:

  • 代码示例的最顶部是否缺少一些代码?
  • 我不这么认为......你是什么意思“缺少代码”?我在 S.O. 上遇到了一些麻烦。代码格式(为什么它不支持围栏代码块?)但我没有故意省略任何东西......我很好奇你的意思?谢谢!
  • 这是 EmberTwiddle 如果有帮助的话:ember-twiddle.com/…

标签: javascript asynchronous ember.js computed-properties


【解决方案1】:

我会替换计算属性,例如

length: Ember.computed('items.length', function() {
  return this.get('items.length');
}),

有别名

length: Ember.computed.alias('items.length'),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多