1

テキスト フィールドとチェックボックスのリストを含むフォームを作成したいと思います。

ステートメントでアンダースコア テンプレート関数を使用すると、_.each()それをレンダリングできます。

保存ボタンをクリックすると、(テキストフィールドを含む) モデルが取得されますが、チェックボックスの配列が本来あるべき場所にありません....

これは私のビューモデルです:

window.SpotEditView= Marionette.View.extend({

    initialize: function () {
        var featureList = ['Rail', 'Curb','Pipe'];
        //this.el = this.compiledTemplate(this.model.toJSON());
        var viewModel = this.model.toJSON();
        viewModel.features = featureList;

        $(this.el).html( _.template(htmlBody, viewModel));
        return;
    },

    render: function () {
        return this;
    },

    events: {
        "change"        : "change",
        "click .save"   : "beforeSave",
        "click .delete" : "delete",
        "drop #picture" : "dropHandler"
    },

    beforeSave: function () {
        var self = this;
        var check = this.model.validateAll();
        if (check.isValid === false) {
            utils.displayValidationErrors(check.messages);
            return false;
        }
        // Upload picture file if a new file was dropped in the drop area
        if (this.pictureFile) {
            this.model.set("picture", this.pictureFile.name);
            utils.uploadFile(this.pictureFile,
                function () {
                    self.save();
                }
            );
        } else {
            self.save();
        }
        return false;
    },

    save: function () {
        var self = this;
        this.model.save(null, {
            success: function (model) {
                self.render();
                var point = model.toMarker();
                app.mapView.model.markers.add(point);
                app.navigate('map', true);
                app.mapView.openInfo(point);

            },
            error: function () {
                utils.showAlert('Error', 'An error occurred while trying to save this item', 'alert-error');
            }
        });
    }
});

これは私のhtmlテンプレートの関連部分です:

<% _.each(features, function(f,key) { %>
<label class="checkbox">
    <input type="checkbox" name="features[<%= key-1 %>]" value="<%= f %>">
    <%= f %>
</label>
<% }); %>

保存関数が受け取るモデルは次のようになります。

{..
    attributes:{ ...
        features:[],
        features[1]:'Dick',
        ...
    }
}

バックボーンの「form-parser」が配列を処理できないようです。

このアプローチに対する修正またはより良い解決策はありますか?

4

0 に答える 0