BackboneJS模板不渲染,节点.js
backbonejs template not rendering, node.js
我一直在通过nodecellar掌握学习骨干js,我已经深入研究了它,并且对数据如何传递的原理有了很好的理解。
我有点卡住了,我正在提取一些 Nodecellar 代码并对其进行修改以掌握我对 Backbone .js的理解。我在渲染列表时遇到问题:
代码如下:
服务器.js
var express = require('express'),
path = require('path'),
http = require('http'),
wine = require('./routes/wines');
users = require('./routes/users');
impulse = require('./routes/impulse');
//dashboard = require('./routes/dashboard');
var app = express();
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser()),
app.use(express.static(path.join(__dirname, 'public')));
});
//impulse management dynamic display
app.get('/', impulse.findAll);
app.get('/impulse', impulse.findAll);
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
因此,我们可以看到服务器是在端口 3000 上设置的。该模型看起来像这样,其中包含一些验证:
型。
window.Impulse = Backbone.Model.extend({
urlRoot: "/impulse",
idAttribute: "_id",
initialize: function () {
this.validators = {};
this.validators.page = function (value) {
return value.length > 0 ? {isValid: true} : {isValid: false, message: "The module needs a url"};
};
this.validators.picture = function (value) {
return value.length > 0 ? {isValid: true} : {isValid: false, message: "The module needs a unique picture"};
};
this.validators.description = function (value) {
return value.length > 0 ? {isValid: true} : {isValid: false, message: "The module needs a description"};
};
},
validateItem: function (key) {
return (this.validators[key]) ? this.validators[key](this.get(key)) : {isValid: true};
},
// TODO: Implement Backbone's standard validate() method instead.
validateAll: function () {
var messages = {};
for (var key in this.validators) {
if(this.validators.hasOwnProperty(key)) {
var check = this.validators[key](this.get(key));
if (check.isValid === false) {
messages[key] = check.message;
}
}
}
return _.size(messages) > 0 ? {isValid: false, messages: messages} : {isValid: true};
},
defaults: {
_id: null,
page: "#",
picture: "users.png",
name: "Impulse Dynamic Engine",
subicon: "fa-exclamation-triangle",
description: "The Impulse view engine has not been set up correctly or has failed to set up. Please contact SandWTech for technical support."
}
});
window.ImpulseCollection = Backbone.Collection.extend({
model: Impulse,
url: "/impulse"
});
路由.js 基于 app.get 功能处理路由,因此我目前只有应用程序中调用的内容.js。
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('engine', server, {safe: true});
db.open(function(err, db) {
if(!err) {
console.log("Connected to the engine database");
db.collection('impulse', {safe:true}, function(err, collection) {
if (err) {
console.log("WARNING 'impulse' collection doesn't exist. Setting up impulse settings");
populateDB();
}
});
}
});
exports.findAll = function(req, res) {
db.collection('impulse', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
主.js 处理路由并决定要显示的内容。
var AppRouter = Backbone.Router.extend({
routes: {
"" : "home",
"impulse" : "home",
"*actions" : "home"
},
initialize: function () {
this.headerView = new HeaderView();
$('.header').html(this.headerView.el);
},
home: function (page) {
var p = page ? parseInt(page, 10) : 1;
var impulseList = new ImpulseCollection();
impulseList.fetch({success: function(){
$("#content").html(new HomeView({model: impulseList, page: p}).el);
}});
this.headerView.selectMenuItem('home-menu');
},
utils.loadTemplate(['HomeView', 'HeaderView', 'WineView', 'WineListItemView', 'AboutView'], function() {
app = new AppRouter();
Backbone.history.start();
});
模型应该传递到主视图并显示:
<a href="#impulse/<%= page %>" class="thumbnail plain" style="text-align: center;">
<img src="<%= picture === null ? 'pics/generic.jpg' : 'pics/' + picture %>" height="150" width="125" alt="">
<h5><%= name %></h5>
<br/>
<i class="<%= subicon %>"></i> <%= description %>
</a>
现在,当我去运行它时,我收到以下错误:
未捕获的类型错误:对象 [对象对象] 没有方法"模板"
错误位于 home 的第 35 行.js它包含 homeView:
window.HomeView = Backbone.View.extend({
initialize: function () {
this.render();
},
render: function () {
var impulses = this.model.models;
var len = impulses.length;
var startPos = (this.options.page - 1) * 8;
var endPos = Math.min(startPos + 8, len);
$(this.el).html('<ul class="thumbnails"></ul>');
for (var i = startPos; i < endPos; i++) {
$('.thumbnails', this.el).append(new HomeViewItemView({model: impulses[i]}).render().el);
}
$(this.el).append(new Paginator({model: this.model, page: this.options.page}).render().el);
return this;
}
});
window.HomeViewItemView = Backbone.View.extend({
tagName: "li",
initialize: function () {
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render: function () {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
它说错误在这里:
$(this.el).html(this.template(this.model.toJSON()));
但我一辈子都不能把它弄出来._.;
为什么模板在全部正确连接时不呈现?我错过了什么?!!!啊
在 HomeView 中,您有以下代码:
new HomeViewItemView({model: impulses[i]})
比在HomeViewItemView中,你有:
$(this.el).html(this.template(this.model.toJSON()));
但是我在HomeViewItemView中看不到模板。它是未定义的。
相关文章:
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- 节点.js将变量显示为 HTML
- 如何解决'窗口未定义'终端上的节点JS出错
- S3文件上传文件对象使用节点js
- 在节点 js 中使用护照本地模块验证(登录)密码时出错
- 嵌套的promise节点js
- 使用节点JS发送电子邮件
- 节点JS重定向循环
- 起始节点.js用于并行的线程池
- 获取图像节点js的主色
- 调整节点js中的图像大小
- 节点JS express和控制台输出到命令行与浏览器
- 节点JS:时间机器坏了——timekeeper.travel不做时间旅行
- 如何访问日期并将其插入数据库?节点.JS&MongoDB
- 节点.js快速删除 req.body 不起作用
- 节点.js中的嵌套异步操作
- 如何在核心php应用程序中使用节点js json Web令牌库
- 使用节点js的Bootstrap和Jquery
- 节点.js加密输入/输出类型
- 节点.js在不可能的地方打印“未定义”