Ember.js PushObject does not insert object into ArrayController

I have the following 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')

      

The problem is I am calling the push object, the length is still 0

. I really can't see what I am doing wrong.

Can anyone see what I am doing wrong? The only thing I can think of is that Content is set to an empty array via Ember.A()

.

I have no idea what else could be.

+3


source to share


2 answers


I'm not really sure where your problem is, as I a) don't know or use CoffeeScript and b) no jsFiddle or working example. But if I read this correctly, you try the following: Check out this jsFiddle which works as expected. Hope you point in the right direction.



+1


source


This is a coffee Script syntax error.
There are two solutions (depending on what you want to implement).

The second example will use the same array for each controller instance.
Also, I recommend calling @_super()

when overriding the init method, otherwise you might get some unexpected results with certain classes.



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

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

      

+1


source







All Articles