我使用Backbone和Backbone表单构建表单。我需要传递可与Bootstrap一起使用的模板,并向标签添加atrribute。看看代码。
有没有办法避免将“altFieldTemplate”添加到每个属性并仅传递一次?
我想避免的另一件事是添加:
editorClass: inputClass,
fieldAttrs:{
class: groupClass
}
无处不在。这没有任何意义。有任何建议如何只做一次? 代码现在可以工作,但这个解决方案不是很优雅。我很感激你的建议。
//input template
var altFieldTemplate =_.template('\
<div class="form-group field-<%= key %>">\
<label class="control-label" for="<%= editorId %>" lang="pl"><%= title %></label>\
<div>\
<span data-editor></span>\
<p class="help-block" data-error></p>\
<p class="help-block"><%= help %></p>\
</div>\
</div>\
');
//terrible way for adding classes
var inputClass = 'form-control span7';
var groupClass = 'form-group control-group span4';
//just repeating to much stuff in this schema
var User = Backbone.Model.extend({
schema: {
name:{
template: altFieldTemplate,
title: 'Imię',
editorClass: inputClass,
fieldAttrs:{
class: groupClass
}
},
secondName:{
template: altFieldTemplate,
title: 'Drugie imię',
editorClass: inputClass,
fieldAttrs:{
class: groupClass
}
},
surname:{
template: altFieldTemplate,
title: 'Nazwisko',
editorClass: inputClass,
fieldAttrs:{
class: groupClass
}
}
}
});
var UserForm = Backbone.Form.extend({});
var form = new UserForm({
model: new User(),
template: _.template($('#formTemplate').html())
}).render();
$('#tab1').append(form.el);