使用.findBy()与Ember数据填充数组控制器 [英] Using .findBy() with Ember-data-populated array controller

查看:98
本文介绍了使用.findBy()与Ember数据填充数组控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些功能测试来测试我的路由器正在导航并正确加载我的模型。到目前为止,甚至在这个问题上也是如此。

I'm writing some functional tests to test that my router is navigating and loading my models correctly. So far, so good--even in light of this issue.

我创建了小提琴,为您的享受。它不起作用 - 尽管分叉 @wagenet ,我从来没有喜欢jsfiddle和ember的运气。但是它有更多的源代码可以帮助我全面了解我所做的一切。

I've created a fiddle, for your enjoyment. It doesn't work--I've never had much luck with jsfiddle and ember, in spite of forking @wagenet. But it has more source code to help get an overall picture of what I have going on.

所以我最大的抱怨是,以下代码无法从控制器中检索具有已知ID的元素:

So my biggest gripe is that the following code doesn't work to retrieve an element with a known id from a controller:

var controller = App.__container__.lookup("controller:postsNew");
var type1Option = controller.get("controllers.types").findBy("TYPE1");

我在setupController钩子中做了类似的工作,它的工作。但是这是在我的应用程序的上下文中,所以它看起来更像这样:

I've done something similar in the setupController hook and it worked. But that was within the context of my application, so it looked more like this:

setupController: function(controller, model) {
    this._super(controller, model);
    this.controllerFor("types").findBy("TYPE1");
}

但即使这样不行了!我现在也在我的应用程序之外工作 - 在一个qunit测试。所以我必须使用 App .__ container __。lookup(),根据我读过的一切。

But even that doesn't work anymore! I'm also working outside my app, now--in a qunit test. So I have to use App.__container__.lookup(), according to everything I've read.

我发现是 controller.length 未定义 - 这是导致 .findBy()失败。并且这些项目存在于数组中...至少可以通过执行 controller.toArray()来看到它们。

What I've found is that controller.length is undefined--which is causing .findBy() to fail. And the items exist in the array...at least, I can see them by doing controller.toArray().

以下是我要做的:

var controller = App.__container__.lookup("controller:postsNew");
var type1Option = null;
$.each(controller.get("controllers.types").toArray(), function(index, elm) {
    if (elm.get("id") === "TYPE1") {
        type1Option = elm;
        return true;
    }
});

这显然不是那么干净。


  • .findBy()破碎

  • 我正在做 .findBy()错误?

  • 如何使用 .findBy() ??

  • Is .findBy() broken?
  • Am I doing .findBy() wrong?
  • How do you use .findBy()??

推荐答案

findBy 需要2个参数,要测试的属性键和要查找的值(如果不传入,则默认为true)。实质上,您正在搜索具有 TYPE1 的属性的模型,即 true

findBy takes 2 arguments, property key to test against, and value to find (it defaults to true if not passed in). Essentially you are searching for a model with a property TYPE1 that's true

你可能想要这样做

findBy("id", "TYPE1")

http://emberjs.com/api/classes/Ember.Array.html#method_findBy


返回具有与传递值匹配的属性的第一个项目。您可以使用目标值传递可选的第二个参数。否则,这将匹配任何评估为true的属性。

Returns the first item with a property matching the passed value. You can pass an optional second argument with the target value. Otherwise this will match any property that evaluates to true.

这篇关于使用.findBy()与Ember数据填充数组控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆