gpt4 book ai didi

ember.js - Ember : Using itemController in ArrayController doesn't work (TypeError: Cannot call method 'lookup' of null )

转载 作者:行者123 更新时间:2023-12-04 23:03:11 26 4
gpt4 key购买 nike

遵循 Ember 上的示例在我的 ArrayController 定义中设置 itemController 会导致阻塞错误消息:

TypeError: Cannot call method 'lookup' of null
JSFiddle: http://jsfiddle.net/jgillick/M4BVV/
// Addresses array controller
App.AddressesController = Ember.ArrayController.extend({
itemController: 'address'
});

// Address object controller
App.AddressController = Ember.ObjectController.extend({
city: function(){
return "San Francisco";
}.property()
});
我发现解决这个问题的唯一方法是......
1) 在#each 处理程序中传递 itemController ( jsfiddle):
{{#each addresses itemController="address"}}
<li>{{line1}}, {{city}}</li>
{{/each}}
...或者...
2) 将容器属性添加到 ArrayController ( jsfiddle):
var addresses = App.AddressesController.create({
container: indexController.get('container'),
content: [
App.Address.create({'line1': '700 Hansen Wy'}),
App.Address.create({'line1': '900 Hansen Wy'})
]
});
这两种解决方案都让人觉得很笨拙而且非常错误。在 ArrayController 本身中设置 itemController 我做错了什么?

最佳答案

itemController从 Controller 和每个 View ,没有关系。
尽管这样做是一样的:将对象包装在指定的 Controller 中。

您的第一个实现抛出 Cannot call method 'lookup' of null , 因为 container AddressesController 的实例一片空白。当您通过其他容器创建 Controller 时,将设置容器。这是为您抽象的,如果您使用:

this.controllerFor('addresses')

代替
 App.AddressesController.create ...

所以这项工作 http://jsfiddle.net/marciojunior/GRaa5/

1) Pass itemController in the #each handler



就像我之前说的, itemController来自 Controller 和每个助手,没有关系。所以你可以混合他。
{{#each addresses itemController="address"}}
<li>{{line1}}, {{city}}</li>
{{/each}}

实际操作 http://jsfiddle.net/marciojunior/spA9Q/

2) Add a container property to the ArrayController



这项工作是因为我之前的解释,

because the container instance of the AddressesController is null



完成 @colymba 示例是可行的,因为在 setupController 之前:
setupController: function(controller, model){
controller.set('content', model);
}

ember 使用 controllerFor 在该 Hook 中提供 Controller ,则容器也存在于生成的 Controller 中。

关于ember.js - Ember : Using itemController in ArrayController doesn't work (TypeError: Cannot call method 'lookup' of null ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18194518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com