【问题标题】:Backbone.js and node.js : how show my data in <select></select> htmlBackbone.js 和 node.js:如何在 <select></select> html 中显示我的数据
【发布时间】: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


【解决方案1】:

我的第一个猜测是您需要有一个适当的下划线模板才能使您的选择发生。像这样的:

<script type="text/template" id="rate_select_template">
    <select id="rate-selector">
        <% rates.each(function(rate) { %>
            <option value="<%= rate.get('duration') %>"><%= rate.get('duration') %></option>
        <% }); %>
    </select>
</script>

【讨论】:

  • 感谢您的关注...我会尝试您的方法是正确的,但是在backbone.js中是麻烦不显示数据...嗯..非常困难T_T..
  • 我试试你的代码,但我有问题在你的代码中获取我的数据库在 mongodb 有一个值定义我需要 2 个定义值名称和 id .. 这是骨干.js 中的新基本代码请高手帮帮我..
  • 您可以在 部分中编写 JSON 代码吗?那将包含您的所有数据集。或者您创建一个发送该数据的 Web 服务,并在您从 Web 服务接收 JSON 时通过编译模板将该数据注入您的模板?
猜你喜欢
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 2018-07-06
  • 2017-10-05
  • 1970-01-01
  • 2019-11-02
相关资源
最近更新 更多