【问题标题】:Ember.js PushObject not inserting object in an ArrayControllerEmber.js PushObject 未在 ArrayController 中插入对象
【发布时间】:2012-03-27 01:33:47
【问题描述】:

我有以下 ArrayController:

Lead.Controllers.Leads = Ember.ArrayController.extend
  init: ->
    content: Ember.A()

    @view = Ember.View.create
      controller: @
      templateName: 'app/templates/leads/list'

    @view.appendTo $('#leads')

  addLead: (data) ->
    lead = Lead.Lead.create()
    lead.setProperties JSON.parse data
    console.log lead.get 'company'
    debugger
    @pushObject lead
    console.log @get('length')

问题是我调用推送对象后,长度还是0。我真的看不出我做错了什么。

谁能看到我做错了什么?我唯一能想到的就是 Content 通过Ember.A() 设置为一个空数组。

我不知道还能是什么。

【问题讨论】:

    标签: coffeescript ember.js


    【解决方案1】:

    我不太确定您的问题出在哪里,因为我 a) 并不真正了解或使用 CoffeeScript,并且 b) 没有 jsFiddle 或工作示例。但是,如果我没看错,那么您尝试执行以下操作:See this jsFiddle 按预期工作。希望这能为您指明正确的方向。

    【讨论】:

    • 谢谢,我没有从 init 调用 _super()。那是我缺少的一点。可惜是需要的。
    【解决方案2】:

    这是一个咖啡脚本语法错误。
    有两种解决方案(取决于您要实现的内容)。

    第二个示例将为控制器的每个实例使用相同的数组。
    另外,我建议在重写 init 方法时调用@_super(),否则您可能会在某些类中得到一些意想不到的结果。

    Ember.ArrayController.extend
      init: ->
        @_super()
        @set 'content', Ember.A()
        # content
    
    Ember.ArrayController.extend
      content: Ember.A()
      init: ->
        @_super()
        # content
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-22
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      相关资源
      最近更新 更多