简体   繁体   English

在Ember模型中的计算属性中使用Promise

[英]Using a Promise in a Computed Property on an Ember model

I have a model, let's call it Task, that has a property assignee. 我有一个模型,我们称它为Task,它有一个财产受让人。 I have another model Admin, that's a set of admins. 我还有另一个模型Admin,那就是一组管理员。 On task, I want to add a property, admin, that looks up the assignee from the admins by email and returns that admin. 在任务上,我想添加一个属性admin,该属性通过电子邮件从管理员那里查找受让人,并返回该管理员。

The primary key on Admin is not email, and in Ember Model, it doesn't look like it's possible to create a belongsTo association on any key other than the primary key. Admin上的主键不是电子邮件,并且在Ember Model中,似乎不可能在除主键之外的任何其他键上创建belongsTo关联。 The reason I send over email rather than an id is that the admin doesn't always exist. 我通过电子邮件而非ID发送邮件的原因是,管理员并不总是存在。

The Task model looks something like this: 任务模型如下所示:

import Em from 'ember';
import Admin from 'project/models/admin';
import PromiseObject from 'project/lib/promise-object';

var Task = Em.Model.extend({
  id: Em.attr(),
  name: Em.attr(),
  assignee: Em.attr(),
  admin: function() {
    return PromiseObject.create({
      promise: Admin.fetch({ email: this.get('assignee') })
    }).then(function(json) {
      return Admin.create(json);
    }, function() {
      return null;
    });
  }.property('assignee'),
  adminName: Em.computed.oneWay('admin.name')
});

export default Task;

PromiseObject is just extending the PromiseProxyMixin, and looks like this: PromiseObject只是扩展PromiseProxyMixin,如下所示:

import Em from 'ember';
export default Em.ObjectProxy.extend(Em.PromiseProxyMixin);

When I try to access the property, I can see the network requests for the admins going across the wire, and I can see the successful response with the correct details included. 当我尝试访问该媒体资源时,我可以看到网络管理员的网络请求,并且可以看到包含正确信息的成功响应。 However, null is returned for the promise. 但是,为承诺返回null

I'm looking to include {{task.adminName}} in my templates. 我希望在模板中包含{{task.adminName}} I'm a little stumped at this point on the best way of resolving the admin promise correctly in my model. 在这一点上,我对在模型中正确解决管理员承诺的最佳方法有些困惑。

You aren't returning the PromiseObject , you're returning a chained promise. 您没有返回PromiseObject ,而是返回了链式承诺。 You should just return the PromiseObject . 您应该只返回PromiseObject

admin: function() {
  var promise = $.getJSON("/admin").then(function(json) {
    return Admin.create(json);
  }, function() {
    return null;
  });

  return PromiseObject.create({
    promise: promise
  });
}.property('assignee'),

Example: http://emberjs.jsbin.com/nobima/12/edit 示例: http//emberjs.jsbin.com/nobima/12/edit

Using Ember Model fetch with an object returns a collection, not a model (unless of course you're fetch isn't Ember Model). 对对象使用Ember Model fetch返回一个集合,而不是一个模型(除非您当然不是Ember Model)。 So json isn't what's being returned at that point. 所以json并不是当时返回的内容。 You probably want to do something along these lines. 您可能想要按照这些思路做一些事情。

admin: function() {
  var promise = Admin.fetch({ email: this.get('assignee') }).then(function(collection){
    return collection.get('firstObject');
  }, function() {
    return null;
  });

  return PromiseObject.create({
    promise: promise
  });
}.property('assignee'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM