小编Gbe*_*her的帖子

具有多个子路径的猫鼬填充路径

想象一下,我有以下型号:

# MODEL A
schemaA = mongoose.Schema
    _bId:
        type: mongoose.Schema.Types.ObjectId
        ref: "B"

# MODEL B
schemaB = mongoose.Schema
    _cId:
        type: mongoose.Schema.Types.ObjectId
        ref: "C"
    _dId:
        type: mongoose.Schema.Types.ObjectId
        ref: "D"

# MODEL C
schemaC = mongoose.Schema
    _eId:
        type: mongoose.Schema.Types.ObjectId
        ref: "E"
Run Code Online (Sandbox Code Playgroud)

模型D和E没有任何其他对象引用,因此不再列出为方便起见.

使用所有引用填充模型"A"的最佳实践是什么?目前我按如下方式解决此任务(它是一个实例方法,因为我经常需要它):

schemaA.methods =
    populateAll: (cb) ->
       @
        .populate
            path:  "_bId"
            model: "B"
            populate:
                path: "_cId"
                model: "C"
                populate:
                    path: "_eId"
                    model: "E"
        , (error) =>
            return cb error, @ if error?
            D.findById @._bId._dId
            .exec (error, d) =>
                return cb error, …
Run Code Online (Sandbox Code Playgroud)

mongoose mongoose-populate mongoose-schema

6
推荐指数
1
解决办法
600
查看次数

如何为 vue 组件模拟 $parent

我如何能够为我的规格模拟 $parent ?在我的组件中使用shallowMount 时,我总是得到未定义的clientWidth/clientHeight。我已经尝试将 $parent 模拟为一个对象,其中 $el 作为键,另外两个嵌套键用于 clientWidth 和 clientHeight,但这并没有按预期工作。我无法弄清楚 parentComponent 的正确用法。

我有一个单一的文件组件,如下所示:

<template>
  <img :src="doSomething">
</template>

<script>
export default {
  name: "Foobar",
  data() {
    return {
      size: null
    };
  },
  computed: {
    doSomething() {
      # here is some string concatenation etc.
      # but not necessary for example
      return this.size;
    }
  },
  created() {
    let parent = this.$parent.$el;
    this.size = `size=${parent.clientWidth}x${parent.clientHeight}`;
  }
};
</script>
Run Code Online (Sandbox Code Playgroud)

创建 vue 应用程序如下所示:

import Vue from "vue";
import Foobar from "./Foobar";

const vueEl = "[data-vue-app='foobar']"; …
Run Code Online (Sandbox Code Playgroud)

vue.js jestjs vue-test-utils

5
推荐指数
1
解决办法
2628
查看次数

ActiveRecord::Base 与 SimpleDelegator 抛出警告

如何删除警告warning: delegator does not forward private method #to_ary

以下是使用给定技术重现问题的代码片段:

  • 红宝石3.2.1
  • 导轨 7.0.4.3
class Foo < ActiveRecord::Base
  def name
    "foobar"
  end
end

class FooDelegator < SimpleDelegator
  def name
    "name"
  end
end

pry(main)> [FooDelegator.new(Foo.new)].flatten
(pry):12: warning: delegator does not forward private method #to_ary
=> [#<Foo:0x00007f3fa3b1ebf0 id: nil, created_at: nil, updated_at: nil>]
Run Code Online (Sandbox Code Playgroud)

编辑:这个问题也发布在 Github 上,因为我认为它实际上属于 ActiveRecord (rails)。

链接: https: //github.com/rails/rails/issues/48059

ruby activerecord ruby-on-rails

5
推荐指数
1
解决办法
243
查看次数