【问题标题】:UI hash problems with Backbone Marionette LayoutBackbone Marionette 布局的 UI 哈希问题
【发布时间】:2013-02-12 17:49:11
【问题描述】:

在下面的Layout 中,我添加了一个CollectionView 以在onRender 中显示一个SELECT 列表。紧接着,我使用 ui 哈希来启用或禁用视图中的所有控件。这不适用于new App.View.Categories 生成的 SELECT。

应该吗?或者 UI 哈希在 Layout 内的 Regions 上不起作用?

App.View.UploadFile = Backbone.Marionette.Layout.extend({
    template: '#upload-file-template',
    regions:{
        category: 'td:nth-child(4)'
    },
    ui:{
        inputs: 'textarea, select, .save'
    },
    onRender: function(){
        this.category.show(
            new App.View.Categories({
                collection: App.collection.categories
            }) // generates the SELECT list
        );

        console.log(this.ui.inputs); // Length 2. Missing select.
        console.log(this.$('textarea, select, .save')); // Length 3

        this.ui.inputs.prop(
            'disabled', (this.model.get('upload_status')!='staged')
        );
    }
});

【问题讨论】:

    标签: backbone.js marionette


    【解决方案1】:

    这应该按照您期望的方式工作。 Marionette 源码中有问题的代码在这里:https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.itemview.js#L49-L51

    对bindUIElements() 的调用是将ui 哈希转换为jQuery 选择器对象,它在onRender 方法被调用之前被调用。

    您是否看到错误?还是选择器只是什么都不返回,对元素没有影响?


    更新:

    啊!当然......我没有足够关注你的代码。您是正确的,因为 UI 元素选择器发生在您将子视图添加到区域之前。我以前从未遇到过这种情况……但这似乎是我们想要修复/支持的事情。

    目前,我建议的最佳解决方法是调用“this.bindUIElements();”在 onRender 方法的最后。这将强制 ui 元素重新绑定到选择器。

    我还将在 github 问题列表中添加一个问题,以寻找更好的解决方案。我不知道我什么时候才能解决这个问题,但这至少会在要修复的问题列表中列出。

    【讨论】:

    • 没有错误。我在上面添加了一些示例 console.logs。选择器忽略了选择,但它返回了其他两个元素。正如您在第二个 console.log 中看到的那样,包含在 $ 中的相同选择器起作用。
    • 看源码,我想我看到了问题。这些项目在 SELECT 项目添加到 onRender 之前绑定。也许在 onRender 中添加这样的区域并不是最佳实践。你有其他模式可以推荐吗?
    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2023-03-28
    • 2018-06-20
    • 2019-04-29
    相关资源
    最近更新 更多