【发布时间】:2013-08-08 01:28:58
【问题描述】:
你好,我是 node.js 和主干.js 的新手,我的开发编程需要你的帮助..
我的文学作品:http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/
我有代码:
node.js // mongodb数据库
server.js
app.get('/class',function(req, res) {
console.log('show all data in class');
db.collection('ak_classroom', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
});
main.js
var AppRouter = Backbone.Router.extend({
routes: {
"class" : "show_class",
},
show_class: function(page) {
var p = page ? parseInt(page, 10) : 1;
var classList = new ClassCollection();
classList.fetch({success: function(){
$("#content").html(new classView({model: classList, page: p}).el);
}});
this.headerView.selectMenuItem('home-menu');
},
});
utils.loadTemplate(['show_class_View'], function() {
app = new AppRouter();
Backbone.history.start();
});
在models.js中
//=== 用于获取我的链接数据库 mongodb get /class
window.class = Backbone.Model.extend({
urlRoot: "/class",
idAttribute: "_id",
//my default variable in databsae
defaults: {
_id: null,
sch_id : "",
cls_name: "",
cls_description: "",
cls_active: "",
}});
//get collection database
window.classCollection = Backbone.Collection.extend({
model: Class,
url: "/class" });
在 classlist.js 中
window.classView = Backbone.View.extend({
initialize: function () {
this.render();
},
render: function () {
var class = this.model.models;
var len = class.length;
var startPos = (this.options.page - 1) * 100;
var endPos = Math.min(startPos + 100, len);
$(this.el).html('<table id="content"><thead><tr><th>ID School</th><th>Name class</th><th>Description</th></tr></thead></table>');
for (var i = startPos; i < endPos; i++) {
$('.content', this.el).append(new show_class_View({model: class[i]}).render().el);
}
return this;
}
});
window.show_class_View = Backbone.View.extend({
tagName: "tr",
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;
}
});
show_class_View.html
<table width="200" border="1">
<tbody>
<tr>
<td><%= sch_id %></td>
<td><%= cls_name %></td>
<td><%= cls_description %></td>
<td><%= cls_active %></td>
</tr>
</tbody>
</table>
这个场景是成功的,但我的问题是如何为
创建数据<select name="cls_name" value="<%= cls_name %>">
<option><%= class[i].cls_name %></option>
</select>
选择数据的数组中的选择类名在哪里??
我是backbone.js 的新手,所以我不知道架构??请帮助我混淆
【问题讨论】:
-
您似乎在服务器端正确,模板端似乎几乎没问题,但为什么我没有看到创建所有 的循环?我现在只看到 1 行。如果要遍历 class[i],则需要一个循环来创建所有选项。
-
是的,这是真的,但我在 html 中声明选择我的数据没有显示未定义的错误数据.. 我很困惑在 html 中声明我的模型数据库的内容..
-
您介意在 jsfiddle 或 plunkr 中设置一个完整的示例吗?到时候我会看的。像这样我不太确定问题出在哪里。有很多潜力。
标签: node.js backbone.js