gpt4 book ai didi

javascript - Handlebars 代码有什么问题?

转载 作者:搜寻专家 更新时间:2023-11-01 00:08:44 25 4
gpt4 key购买 nike

嘿,所以我正在尝试从下划线切换到 Handlebars ,但模型没有渲染任何内容,但模板正在正确更改。此外,当通过单击编辑按钮显示 editTemplate 时,#{firstName} 和其他显示为未定义。

在我的布局 jade 文件中,我包含了所有合适的文件,jquery、underscore、backbone 和 handlebar。

这是我的 main.js 文件

(function () {
window.App = {
Models: {},
Collections: {},
Views: {},
// Templates: {},
Router: {}

};

// MODEL
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.';
}
},

initialize: function() {
this.on('invalid', function (model, invalid) {
console.log(invalid);
});
}

});



//VIEW
App.Views.User = Backbone.View.extend({
model: App.Models.User,
el: 'user',
//tagName: 'div',
//id: 'user',
//className: 'userProfile',


initialize: function (){

},

render: function() {
var template = Handlebars.compile($("#userTemplate").html());
var editTemplate = Handlebars.compile($("#userEditTemplate").html());

this.$el.html(this.template(this.model.toJSON()));
return this;
},

events: {
'click button.edit': 'editProfile',
// 'click button.save': 'saveEdits',
'click button.cancel': 'cancelEdits'
},

editProfile: function () {
this.$el.html(this.editTemplate(this.model.toJSON()));

},


cancelEdits: function() {
this.render();
}

});
//start history service
Backbone.history.start();

var user = new App.Views.User({model: new App.Models.User()});
user.render();
})();

这是我的 Jade 文件

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#user
p #{firstName} #{lastName}
p #{email}
p #{phone}
p #{birthday}
button.edit Edit



script(id="userTemplate", type ="text/template")
p #{firstName} #{lastName}
p #{email}
p #{phone}
p #{birthday}
button.edit Edit

script(id="userEditTemplate", type ="text/template")
div
form(action="#")
input(type="text", class="firstName", value=#{firstName}) input(type="text", class="lastName", value=#{lastName})
input(type="email", class="email", value=#{email})
input(type="number", class="phone", value=#{phone})
input(type="date", class="birthday", value=#{birthday})
button.save Save
button.cancel Cancel
hr

布局 Jade

doctype 5
html
head
title=title
link(rel='stylesheet', href='/css/style.css', type='text/css')
link(rel='stylesheet', href='/css/bootstrap-responsive.css')
link(href='/css/bootstrap.css', rel='stylesheet', type='text/css')
link(href='/css/font-awesome.min.css', rel='stylesheet', type='text/css')
script(src='/js/jquery.min.js', type='text/javascript')
script(src='/js/jquery.validate.min.js', type='text/javascript')
script(src='/js/script.js', type='text/javascript')
script(src='/js/underscore.min.js', type='text/javascript')
script(src='/js/backbone.min.js', type='text/javascript')
script(src='/js/handlebars.js', type='text/javascript')
body
div#container
div#header
block content
include footer

最佳答案

如果有人想看答案,基本就是这样调用的错误

#{firstName}

而不是正确的用法是:

{{firstName}}

关于javascript - Handlebars 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17584780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com