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