0

你好我是node.js和backbone.js的新手我需要你帮助我的开发编程..

我的文献:http ://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/

我有代码:

node.js // MongoDB数据库

服务器.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(); 
});

在模型.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的新手,所以我不知道架构??请帮助我很困惑

4

1 回答 1

0

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

<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>
于 2013-08-07T17:28:58.837 回答