Emberjs - 一起使用CollectionView和ItemController

时间:2013-03-23 14:48:03

标签: javascript ember.js collectionview

我的模型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> 

感谢您的帮助。真的很感激。

2 个答案:

答案 0 :(得分:2)

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

为此,您必须明确DocumentsController

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

然后在您的类别中:

App.CategoryController = Ember.ObjectController.extend({
    needs: ['documents']

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

答案 1 :(得分:0)

我认为这是Ember中的一个错误,即将解决:

https://github.com/emberjs/ember.js/issues/4137