dagda1
dagda1

Reputation: 28898

Where to put an Ember view for each response from a websocket

I have the following ember controller that is going to wait for json objects to be passed from a websocket server via the onmessage handler:

Lead.Controllers.ParsingController = Ember.Object.extend
  start_parsing: (url_search) ->
    socket = new Lead.WebSocket("ws://#{document.domain}:61615")

    socket.onopen = (evt) ->
      socket.send url_search.search_url

    socket.onmessage = (evt) ->
      Lead.leads_controller.addLead evt.data

My LeadsController currently looks like this:

Lead.Controllers.Leads = Ember.Object.extend

  addLead: (lead) ->    
    @view = Ember.View.create
      controller: @
      #etc.

My questions are these:

  1. Am I right not to use an ArrayController because they are only to be used for collections? Sorry for the obvious question but I just want to check.
  2. If I create a new view, everytime the addLead method is called, will I need to keep a reference to each view in an internal array and iterate over the array calling dispose on each child view when I am disposing of the main view?
  3. Would I be better off creating a separate controller for each child view that is appended? I am guessing no but would like to check.

Any help or guidance on these questions would be greatly appreciated.

Upvotes: 1

Views: 438

Answers (1)

Luke Melia
Luke Melia

Reputation: 8389

I'm not going to answer your specific questions, but rather suggest an alternate implementation that I think would be more idiomatic Ember and be simpler.

It's hard to tell what your broader view layer looks like, but I based on my guess at your problem domain, I would think that you would have a #each helper or CollectionView that is databound to a leadsController which subclasses ArrayProxy. When the json comes in from the websocket, call pushObject on the leadsController. Bindings will automatically update and render the new lead in the view output.

If I misunderstood the app's functionality, please do clarify.

Upvotes: 4

Related Questions