Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js在回调中返回computed属性_Ember.js_Callback_Handlebars.js_Computed Properties - Fatal编程技术网

Ember.js在回调中返回computed属性

Ember.js在回调中返回computed属性,ember.js,callback,handlebars.js,computed-properties,Ember.js,Callback,Handlebars.js,Computed Properties,我有一个计算属性,用于检查用户是否已经喜欢帖子: likeable: Ember.computed(function () { const currentUser = this.get('auth.credentials.id'); const post = this.get('post.id'); // Get likes and filter by post and current user this.get('store').query('like'

我有一个计算属性,用于检查用户是否已经喜欢帖子:

  likeable: Ember.computed(function () {
    const currentUser = this.get('auth.credentials.id');
    const post = this.get('post.id');

    // Get likes and filter by post and current user

    this.get('store').query('like', {
      filter: {
         user_id: currentUser,
         post_id: post
       }
    }).then(function(like) {

      // Check if any likes were returned

      if (like.get('length') != 0) {
        console.log('length is not 0')
        return false
      } else if (like.get('length') === 0) {
        console.log('length is 0')
        return true
      }
    })
  })
它在我的模板文件中被调用,如下所示:

{{#if likeable}}
  <button class="like-button" {{action 'addLike' post}}>Like This Post</button>
{{/if}}
{{{#如果喜欢}
喜欢这个帖子吗
{{/if}

我能够看到console.logs,我知道正在检索正确的记录。但是,问题是,
likeable
的值没有更新,按钮也没有显示。我很确定这是因为我的条件在回调中,并且没有将true返回到实际的computed属性。有办法解决这个问题吗

运行此函数后,您是否尝试过console.logging likeable?我想知道这是否是一个异步问题。在将Likable设置为true或false后,您可能必须再次获取帖子,或者将此逻辑移动到后端序列化程序,并在那里设置Likable的值。

再三考虑,我认为应该在您的Ember模型上定义
Likable
属性
你要回来的布尔人哪儿也去不了。您需要返回布尔值解析的承诺

您也可以尝试一个
async
版本,但我还没有测试过,它可能无法工作

likeable: Ember.computed(async function() {
  const post_id = this.get('post.id')
  const user_id = this.get('auth.credentials.id')

  // get likes and filter by post and current user
  const like = await this.get('store').query('like', {
    filter: {
      post_id,
      user_id
    }
  })

  return like.get('length') === 0
})

您不应该考虑计算属性中的异步行为,Ember通过运行循环管理所有计算属性,这意味着它不会等待计算属性解析承诺

处理此问题的方法是进行异步调用并设置属性,这会产生副作用,可能是在
didInsertElement
hook期间:

import Component from '@ember/component';
export default Component.extend({
    didInsertElement() {
        this._super(...arguments);
        this.fetchLikesFromServer().then(like => {
            this.set('likeable', like.get('length') === 0);
        });
    },
});

正如其他答案所指出的那样,计算财产和承诺不能很好地发挥作用。-看看这个,这解释了如何做得很好。基本上,要么使用并发任务,要么使用特殊的承诺支持对象。我每次都会执行并发任务

正如alizahid所说,您的计算属性没有返回值,内部承诺中的返回语句只返回到承诺链,而不会到达属性的外部

尝试使用
return
从您的财产中返回承诺结果:

  likeable: Ember.computed(function () {
    const currentUser = this.get('auth.credentials.id');
    const post = this.get('post.id');

    // Get likes and filter by post and current user

    return this.get('store').query('like', {
      filter: {
         user_id: currentUser,
         post_id: post
       }
    }).then(function(like) {

      // Check if any likes were returned

      if (like.get('length') != 0) {
        console.log('length is not 0')
        return false
      } else if (like.get('length') === 0) {
        console.log('length is 0')
        return true
      }
    })
  })
如果这没有帮助,您可能必须将承诺链包装到一个
DS.PromiseObject
(请参阅),这就是ember数据处理承诺返回模板的方式

  likeable: Ember.computed(function () {
    const currentUser = this.get('auth.credentials.id');
    const post = this.get('post.id');

    // Get likes and filter by post and current user

    return DS.PromiseObject.create({
      promise: this.get('store').query('like', {
        filter: {
          user_id: currentUser,
          post_id: post
        }
      }).then(function (like) {

        // Check if any likes were returned

        if (like.get('length') != 0) {
          console.log('length is not 0')
          return false
        } else if (like.get('length') === 0) {
          console.log('length is 0')
          return true
        }
      })
    });
  })

console.log('type is',typeof like.get('length')返回'type is number',使用else返回与之前相同的结果此方法的问题是,在函数之后返回likeableValue总是返回false。我很确定这是因为检查类似项的函数是异步的。