【发布时间】:2016-04-12 05:58:35
【问题描述】:
我在翡翠中使用下划线。
index.jade
extends layout
block content
.wrap
.container-fluid#container
script(type='text/template' id='main-template')
.row
.col-xs-8.col-sm-9
.heading h2 Welcome to the Node Chatroom
hr/
.content
ul.list-group#chatList
.col-xs-4.col-sm-3
.sidePanel
.panel-heading
h3.panel-title Online Users
span.badge pull-right#userCount
hr/
.panel-body
ul.list-group#userList
.push
.footer
.container-fluid
.row
form
.form-group
.col-xs-8.col-sm-9
input(type="text" id="chatInput" class="form-control input-lg" placeholder="Write a message here..." rows="3")
.col-xs-4.col-sm-3
button(id="send-message-btn" class="btn btn-primary btn-lg btn-block" type="submit") Send Message
script(type='text/template' id='login-template')
.row
.col-md-4.col-md-offset-4.text-center
.login
.form-group
label Enter name
input(type="text" class="form-control" id="nameText")
.btn-group
button(id="name" type="button" class="btn btn-primary btn-lg") Login
Main.js
var LoginView = Backbone.View.extend({
template: _.template($('#login-template').html()),
events: {
'click #name': 'onLogin'
},
initialize: function(options) {
this.vent = options.vent;
this.listenTo(this.model, "change:error", this.render, this);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
onLogin: function() {
//this.l.start();
this.vent.trigger("login", this.$('#nameText').val());
}
});
它在 template: _.template($('#login-template').html()), 行显示 TypeError: n is undefined
我尝试过使用 .在jade doc 中提出的脚本之后。但它没有用。
看起来下划线无法访问#login-template id。有什么原因吗?
【问题讨论】:
-
你是在导入下划线吗?如果你不是,你应该在你的主文件中做。
var _ = require("underscore"); -
如果您无法访问
#login-template,那么您会收到一个错误消息,抱怨undefined没有replace方法或类似的方法。你看过$('#login-template').html()看看有什么吗?模板中没有下划线标记,为什么要给模板函数数据? -
尝试移动模板:_.template($('#login-template').html()),以渲染函数。 like this.template: _.template($('#login-template').html())
-
@muistooshort 是的,它有 #login-template 的内容。它在下一个模板中。
-
我看到了一些 Jade 的东西,但
$('#login-template').html()实际上给了你什么?
标签: node.js backbone.js underscore.js pug