【问题标题】:Backbone/Underscore: How to get index of a model inside render?骨干/下划线:如何在渲染中获取模型的索引?
【发布时间】:2015-05-06 21:21:29
【问题描述】:

我是主干和下划线的新手,我正在尝试在渲染时获取模型索引以在模板中进行一些命名。 这篇文章有助于找到事件索引, Backbone.js: How to get the index of a model in a Backbone Collection?

但是如何在渲染或初始化时找到模型索引?

  var SectionView = builder.classes.ItemView.extend({

    template: _.template(
        '<div class="pb-item-type-column pb-item custom-section">' +
            '<div class="panel fw-row">' +
                '<div class="panel-left fw-col-xs-6">' +
                    '<div class="column-title">Grid <%- item_index %></div>' +
                '</div>' +
                '<div class="panel-right fw-col-xs-6">' +
                    '<div class="controls">' +
                        '<i class="dashicons dashicons-edit edit-section-options"></i>' +
                        '<i class="dashicons dashicons-admin-page custom-section-clone"></i>' +
                        '<i class="dashicons dashicons-no custom-section-delete"></i>' +
                    '</div>' +
                '</div>' +
            '</div>' +
            '<div class="builder-items"></div>' +
        '</div>'
    ),
     events: {
        'click': 'onSectionWrapperClick',
        'click .edit-section-options': 'openSectionEdit',
        'click .custom-section-clone': 'cloneItem',
        'click .custom-section-delete': 'removeItem',
     },
     initialize: function() {
        this.defaultInitialize();

     },
     render: function() {
        this.defaultRender({

           item_index: 'this is where I need the index'
        });

     },

     cloneItem: function(e) {
        e.stopPropagation();

        var index = this.model.collection.indexOf(this.model),
           attributes = this.model.toJSON(),
           _items = attributes['_items'],
           clonedSection;

        delete attributes['_items'];

        clonedSection = new Section(attributes);
        this.model.collection.add(clonedSection, {
           at: index + 1
        });
        clonedSection.get('_items').reset(_items);

     },

     openSectionEdit: function() {
        this.modal.open();
     },
     removeItem: function() {
        this.remove();

        this.model.collection.remove(this.model);
     },

  });

  var Section = builder.classes.Item.extend({

     defaults: function() {
        var defaults = _.clone(localized.defaults);

        defaults.shortcode = fwThzSectionBuilder.uniqueShortcode(defaults.type + '_');

        return defaults;
     },
     initialize: function() {
        this.defaultInitialize();

        /**
         * get options from wp_localize_script() variable
         */
        this.modalOptions = localized.options;

        this.view = new SectionView({
           id: 'fw-builder-item-' + this.cid,
           model: this
        });

     },

     allowIncomingType: function(type) {
        if (type == 'columns') {
           return true;
        } else {
           return false;
        }
     }

  });

如果我可以至少让父级渲染它会进一步帮助我,但这也不起作用

$(this.el).parent();

我正在扩展这个https://github.com/ThemeFuse/Unyson-Builder-Extension/blob/master/includes/option-types/builder/static/js/builder.js

更新和解决方案:

由于这不是我的应用程序,而且我正在扩展现有应用程序,因此很难找到我的问题。我的问题是应用程序创建者在初始化和渲染时启动了超时

https://github.com/ThemeFuse/Unyson-Builder-Extension/blob/master/includes/option-types/builder/static/js/builder.js#L348-L350

因此,我很早就开始了收藏搜索。

Morslamina 的回答和引用的帖子都是正确的。

所以对于正在寻找 index 的你来说,给你的 index search 一些超时时间并检查一下。收藏品必须在那里。

【问题讨论】:

  • 而this.model.get('id') 不起作用?
  • @StephenThomas, id works ,但该系列是我需要的,但它不可用。我认为他们没有绑定(this)在该应用程序中进行初始化。在 (stupid down vote troll) ,你为什么要对一个完全有效的问题投反对票,甚至不合作?
  • @StephenThomas,不,this.model.get('id') 也不起作用,我“看到”了模型,但除了 cid 之外什么都没有,看起来他们做了一些重置在渲染github.com/ThemeFuse/Unyson-Builder-Extension/blob/master/… 我无法到达这个prntscr.com/72gmpc

标签: jquery backbone.js underscore.js


【解决方案1】:

已分配给集合的每个模型都应具有引用父集合的属性this.collection。从那里,获取模型的索引应该很简单。

render: function(){
    index = this.model.collection.indexOf(this.model)
}

请注意,如果您已将模型添加到多个集合,这可能会变得更加复杂。在这种情况下,您需要引用您想要索引的特定集合,因为 Backbone 不会自动知道您在谈论哪个集合。

编辑:在仔细查看您的代码后,我认为您遇到了范围界定问题。如果你真的在打电话

this.defaultRender({
   item_index: this.model.collection.indexOf(this.model)
});

然后this 的作用域是您传递给 defaultRender 的对象,而不是视图。

试试吧

index = this.model.collection.indexOf(this.model)
this.defaultRender({
   item_index: index
});

【讨论】:

  • 我已经尝试过了,并引用了建议相同 sn-p 的帖子。是的,模型在多个集合中使用,但我似乎无法找到正确的方法来索引它
  • 如果您的模型在多个集合中使用,您必须将您的视图传递给您想要索引的集合的引用,因为this.model.collection 不一定持有正确的引用。
  • 你介意展示一个例子吗,我发布了我拥有的两个扩展。
  • 引用必须来自你实例化你的集合的任何地方,我不确定我是否看到你在上面的代码中做了那个。它可能在您的主应用程序文件中。我很乐意提供更多帮助,但我对您的代码应该做什么以及应该在哪里做感到困惑。
  • 另外,如果 this.model.collection 在您的 cloneItem 函数中工作,它应该在您的 render 函数中同样有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多