问题描述
我正在编写一些功能测试来测试我的路由器正在导航并正确加载我的模型。到目前为止,甚至在这个问题上也是如此。
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.
我创建了,为您的享受。它不起作用 - 尽管分叉,我从来没有喜欢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")
这篇关于使用.findBy()与Ember数据填充数组控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!