1
votes

I have a simple ember-data model called a Task. When I route to /tasks, I can iterate over each task to create a list of all the tasks via my TasksController, template, and autogenerated view:

{{#each task in filteredContent}}
  {{ partial "taskItem" }}
{{/each}}

filteredContent is a property watching the content and a filter criteria to provide a subset of App.Task.find().

This works well, but now I'd like to add more functionality to my tasks list, namely drag-and-drop sorting via jQuery's sortable plugin. Since I'd like to encapsulate the logic for displaying the tasks, I created a TasksListController:

App.TasksListController = Ember.ArrayController.extend {
  needs: ['tasks']
  contentBinding: 'controllers.tasks.filteredContent'
}

a TasksListView:

App.TasksListView = Ember.CollectionView.extend {
  tagName: 'ul'
  classNames: ['tasks-list']
  itemViewClass: App.TasksListItemView

  didInsertElement: ->
    this._super();
    this.$().sortable().disableSelection();
}

and a TasksListItemView:

App.TasksListItemView = Ember.View.extend {
  templateName: 'tasks-list-item'
  classNames: ['task-item']
}

If I insert console.log this.get 'controller.firstObject' in the didInsertElement method, the output is correct, but {{render tasks-list}} in the Tasks template (operating under the TasksController if I understand things correctly) always seems like there is no content. I've come across a few examples using CollectionView, but they seem to be pre-RC1, and suggest doing something like {{view 'App.TasksListView' contentBinding=filteredContent'}} (which doesn't seem to work either).

In this situation, is the render or view helper the appropriate way to go? Is this the right way to approach this problem, or is there a better Ember pattern to follow?

I am using Ember.js RC2 and ember-data DS.FixtureAdapter (revision 12). A simplified fiddle is available at http://jsfiddle.net/superlou/sUUXj/.

2
Does {{render tasksList}} behave similarly? - Christopher Swasey
Unfortunately, I can't get to the computer with the full code right now, but I created a simplified fiddle that seems to have the same result. In the fiddle, I call the controller simply List, to avoid any ambiguity over tasks-list, "TasksList", tasks_list, etc. - superlou
{{render tasksList}} does behave similarly. - superlou

2 Answers

4
votes

It turns out to be relatively straightforward. The CollectionView does not automatically inherit content from the controller, so the view must be modified to include:

App.TasksListView = Ember.CollectionView.extend {
    contentBinding: 'controller'
    ...

The content can then be accessed in the template like so:

{{#linkTo task view.content}}
    {{#with view.content}}
        <div class="task-item">
            {{name}}
        </div>
    {{/with}}
{{/linkTo}}

Many thanks to panoptic at Ember Discuss.

0
votes

I wrote a jsbin to show how you can implement that easily.

http://emberjs.jsbin.com/yideji/2/edit

App.TasksListView = Ember.CollectionView.extend({
  itemViewClass: App.TasksListItemView,

  // A `CollectionView` expects its data in the `content`
  // property. But the route will set the `controller` on the view, not the `content`.
  // Using bindings allows to make that transparent to the developer.
  contentBinding: 'controller'
});

The nice thing you can add is in your item view:

App. TasksListItemView = Ember.View.extend({      
  // A view applys its `context` to the template. The `context` is usually its `controller`.
  // In the case of an item of a `CollectionView`, `context` isn't set, but `content` is.
  // So we just bind `context` to `content` so we don't have to write `view.content.myValue`
  // but `myValue` in the template
  contextBinding: 'content'
})

Adding this will allow you to simplify your template:

{{#linkTo task this}}
    <div class="task-item">
        {{name}}
    </div>
{{/linkTo}}