如何在BackboneForms模式中初始化rails-select2的选择? [英] How to initialize the selection for rails-select2 in BackboneForms schema?

查看:97
本文介绍了如何在BackboneForms模式中初始化rails-select2的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该项目使用 marionette-rails select2-rails 和此移植到BackboneForms的端口,以提供多选表单字段.选择选项对用户可用.从包含选项总列表的集合中检索它们:

The project uses marionette-rails, backbone-on-rails, select2-rails and this port to BackboneForms to provide a multiselect form field. The select options are available to the user. They are retrieved from the collection containing the total list of options:

MyApp.module("Products", function(Products, App, Backbone, Marionette, $, _) {

  Products.CustomFormView = Products.CustomView.extend({

    initialize: function(options) {
      this.model.set("type", "Product");
      Products.EntryView.prototype.initialize.apply(this, arguments);
    },

    schemata: function() {
      var products = this.collection.byType("Product");
      var productTypes = products.map(function(product){
        return {
          val: product.id,
          label: product.get("name")
        };
      });

      return {
        productBasics: {
          name: {
            type: "Text",
            title: "Name",
            editorAttrs: {
              maxLength: 60,
            }
          },
          type: {
            type: 'Select2',
            title: "Product type",
            options: {
              values: productTypes,
              value: [3, 5],
              initSelection: function (element, callback) {
                var data = [];
                $(element.val().split(",")).each(function () {
                  data.push({id: this, text: this});
                });
                callback(data);
              }
            },
            editorAttrs: {
              'multiple': 'multiple'
            }
          }
        }
      };
    }

  });
});

我是否在options.value中正确初始化了value?为何从未调用initSelection?我从文档中复制了该功能-对于我的情况,该功能可能不完整. ID为35的产品都不会显示为选择.

Do I initialize the value correctly in options.value? How comes initSelection is never called? I copied the function from the documentation - it might be incomplete for my case. None of the products with the IDs 3 and 5 is displayed as the selection.

推荐答案

initSelection仅在异步加载数据时使用.我的理解是,如果您使用数组作为Select2控件的数据源,则无法在初始化时指定选择.

initSelection is only used when data is loaded asynchronously. My understanding is that there is no way of specifying the selection upon initialization if you are using an array as the data source for a Select2 control.

初始化选择的最佳方法是在创建表单后使用setValue.这是一个基于示例代码的简化示例.

The best way of initializing the selection is by using setValue after the form is created. Here is a simplified example based on the code in your example.

var ProductForm = Backbone.Form.extend({
  schema: {
    type: {
      type: 'Select2',
      title: "Product type",
      options: {
        values: productTypes,
      },
      editorAttrs: {
        'multiple': 'multiple'
      }
    }
  });

var form = new ProductForm({
  model: new Product()
}).render();

form.setValue("type", [3, 5]);

这篇关于如何在BackboneForms模式中初始化rails-select2的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆