Emberjs - 一起使用CollectionView和ItemController

ray*_*ley 5 javascript collectionview ember.js

我有一个Category有很多的模型Documents.渲染个人时,Category我想documents在拖放可排序列表中列出所有子项.我还想双击任何个人document以允许该文档的内联编辑.

我让两个部分都在那里工作,但似乎无法弄清楚如何将它们合并在一起.

对于可排序列表,我使用自定义子类CollectionView来渲染documents,并在插入元素后调用html5sortable jquery插件.

对于内联编辑,我itemController为每个document渲染设置了一个.在DocumentController我维护的应用程序状态下编辑文档.

我正在寻找有关如何结合这两种方法的见解.我认为我需要的是一种itemController为每个人设置一个itemView的方法CollectionView.我已将相关代码放在下面.

App.SortableView = Ember.CollectionView.extend({
    tagName: 'ul',
    itemViewClass: 'App.SortableItemView', 

    didInsertElement: function(){
        var view = this;
        Ember.run.next(function() {
        $(view.get('element')).sortable();
        });
    }
});

App.SortableItemView = Ember.View.extend({
    templateName: 'sortable-item',
    doubleClick: function() {
        //This should ideally send 'editDocument' to controller
    }
});

App.DocumentController = Ember.ObjectController.extend({
    isEditing:false,
    editDocument: function () {
        this.set('isEditing', true);
    },
    finishedEditing: function() {
        var model = this.get('model');
        model.get('store').commit();
        this.set('isEditing', false);
    }
});

<script type="text/x-handlebars" data-template-name="category">
    <h1>{{ name }}</h1>

    <h2>Documents</h2>
    <!-- This makes a sortable list -->
    {{view App.SortableView contentBinding="documents"}}

    <!-- This makes an editable list -->
    {{#each documents itemController="document"}}
        <!-- change markup dependent on isEditing being true or false -->
    {{/each}}

    <!-- How do I combine the two -->
</script> 
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.真的很感激.

Chr*_*sey 2

秘密是itemController在你的视图上设置ArrayController而不是试图在视图上设置它。然后,绑定到该 ArrayController 的任何视图都将返回一个控制器,而不是其后面的任何内容。

为此,您必须明确表示DocumentsController

App.DocumentsController = Ember.ArrayController.extend({
    needs: ['category'],
    contentBinding: 'controllers.category.documents',
    itemController: 'document'
});
Run Code Online (Sandbox Code Playgroud)

然后在您的类别中:

App.CategoryController = Ember.ObjectController.extend({
    needs: ['documents']
Run Code Online (Sandbox Code Playgroud)

现在,在您的模板中,绑定到controllers.documents而不是documents.