我使用带有骨干模型粘合剂的骨干木偶。
使用下面的代码,如果 select + input[type=date] 的默认值已完成并且用户没有更改它们,则当用户单击保存时模型属性将返回为空。如果用户在字段上编辑并单击保存,则在模型属性上设置一个字段。
define(["marionette", "underscore", "text!app/templates/sicknesses/form.html", "app/models/sickness", "app/collections/users"], function(Marionette, _, Template, Model, Users) {
"use strict"
return Backbone.Marionette.CompositeView.extend({
events: {
"click #createButton": "onClickSave"
},
_modelBinder: undefined,
initialize: function(options) {
var that
this._modelBinder = new Backbone.ModelBinder()
this.mode = 'create'
this.users = new Users()
this.users.on("reset", this.render, this)
this.users.fetch()
this.model = new Model()
},
onClickSave: function(ev) {
ev.preventDefault()
console.log(this.model.attributes)
},
render: function() {
var bindings, html
if (this.users.length > 0) {
bindings = {
start_date: "#start_date",
end_date: "#end_date",
sicknote: "#sicknote",
user: "#user"
}
html = _.template($(Template).html(), {
model: this.model.toJSON(),
users: this.users.toJSON(),
mode: this.mode
})
this.$el.html(html)
this._modelBinder.bind(this.model, this.$el, bindings)
}
return this
}
})
})