1

我有一个 ArrayController 如下:

DashboardController = Ember.ArrayController.extend
  content: []

  xFormatter: (x) ->
    d3.time.format("%m-%d") x

  init: ->
    console.log("dashboardController init")
    @_super()

    y = undefined
    currentDate = undefined
    i = 1

    while i < 30
      currentDate = new Date("12/" + i + "/2011")
      @content.pushObject
        x: currentDate
        y: (Math.random() * 100) / 10

      i++

当应用程序初始化时,控制器被创建(如控制台所示)并且内容在 期间被推送到数组中init,但是content当我尝试从其他地方(例如视图)访问它时未定义。我试过了content: null,然后@set("content", [])在开始时做,init但得到了相同的结果。

contentEmber 指南说在我的路由方法中设置一个 ArrayController 的属性setupController来告诉它要表示什么模型,但是由于这个“模型”实际上是一个函数的结果,我不知道该怎么做。我认为由于init为每个创建的实例运行,每个实例都会在content不使用setupController.

我认为我从中提取的示例是使用不适合 1.0.0-rc1 的旧方法。我应该如何重组它才能工作?

4

2 回答 2

1

A simple fix ( as of EmberJS 1.0.0rc-3 ) is to define the model in the route to the controller content in cases where it already exists. Like So (pardon the coffee script):

App.MyRoute = Em.Route.extend
  model: ->
    @get('controller.content') || App.My.find(query)

There was a change in master though, that prevents this from working, so you might need to do

App.MyRoute = Em.Route.extend
  model: ->
    controller = @controllerFor('my')
    controller.get('content') || App.My.find(query)
于 2013-04-25T18:02:46.593 回答
0

好的,看来诀窍是将生成值的代码移动到model路由的钩子中,然后使用setupController钩子设置控制器的content. 我唯一不喜欢的是,每次转换到路由时都会生成数据(因为每次都会调用modeland钩子)。setupController我想让它计算一次并缓存。我想这样做的方法是创建一个对象,它要么第一次创建新数据,要么返回缓存的数据,然后在model挂钩中调用这个对象。

于 2013-03-03T22:03:53.470 回答