3
所以我试图让我的骨干模型中的字段在视图中被调用,由模型进行编辑和验证。我该如何去做呢?我知道有一种做contenteditable =“true”的html方式,但我正在寻找一种更加以骨干为导向的方式,或者一种实际上也能够验证的方式。如何在骨干视图中创建内联编辑?
这是我的main.js文件的当前代码(但我没有试图.append它,我希望它留在原地,并试图找出如何让字段被具体调用取决于哪文本他们点击最后按钮应该改变太多(保存更改)
的main.js
App.Models.User = Backbone.Model.extend({
defaults: {
firstName: 'first',
lastName: 'last',
email: 'Email',
phone: '222',
birthday: 'date'
},
validate: function (attrs) {
if (!attrs.firstName) {
return 'You must enter a real first name.';
}
if (!attrs.lastName) {
return 'You must enter a real last name.';
}
if (attrs.email.length < 5) {
return 'You must enter a real email.';
}
if (attrs.phone.length < 10 && attrs.phone === int) {
return 'You must enter a real phone number, if you did please remove the dash and spaces.';
}
if (attrs.city.length < 2) {
return 'You must enter a real city.';
}
}
});
App.Views.UserUpdate = Backbone.View.extend({
model: App.Models.User,
events: {
'click .edit': 'editUserProfile'
},
editUserProfile: function(field) {
var field =
$('#user').append('<input type="text" class="edit" placeholder="' + field+'" /> ');
},
initialize: function() {
this.model.on('change', function() {
this.render();
}, this);
},
render: function() {
this.$el.html(this.model.get('email'));
}
});
这是玉文件:。
extends layout
block content
div.centerContent
script(type="text/javascript", src="/js/main.js")
h4 User goes here with equal before it no space
div(contenteditable="true")
form#user
- var firstName = "John"
- var lastName = "Smith"
label #{firstName} #{lastName}
- var email = "[email protected]"
label.edit #{email}
- var phone = "555-555-5757"
label #{phone}
- var pin = "PIN: LIO20001"
label #{pin}
- var birthday = "07/28/1982"
label #{birthday}
button Post
hr
div.user User
p
button.edit Edit
骨干像任何MVC将允许你有相同型号的多个视图,你可以交换到编辑查看点击等。看看这个例子http://nettuts.s3.amazonaws.com/1147_bb3and4/4/demo/index.html?# – irfn
了解,但试图做一个花哨的内联编辑,当你点击你看看http://etchjs.com/#demo这个词是 – Lion789
。这也使用contenteditable,但很好地与Backbone集成 – irfn