バックボーン、エクスプレス、および mongodb は初めてです。フィールドごとにmongodbコレクションを検索するためにクエリ文字列を渡そうとしています。私は何か間違ったことをしています。ルーターから「フェッチ」をコメントアウトすると、ページが見つかります。フェッチしようとすると、ページが見つからないというエラーが発生します。どこが壊れているのかを特定しようとしましたが、バックボーン アーキテクチャはまだ混乱しています。前もって感謝します。(mongodb 呼び出しの構文の問題だと思います)
これが私のコードです。
この URL は、"type" = 3 のコレクションを返す必要があります。
localhost:8888/#content/3
モデル/models.js:
window.Content = Backbone.Model.extend({
urlRoot: "/content",
idAttribute: "_id"
});
window.ContentCollection = Backbone.Collection.extend({
model: Content,
url: "/content"
});
ビュー/content.js
window.ContentListView = Backbone.View.extend({
initialize: function () {
this.render();
},
render: function () {
//return this;
this.$el.append('<ul class="thumbnails">');
this.collection.each(function(model) {
this.$('.thumbnails').append(new ContentView({model: model}).render().el);
}, this);
return this;
} });
window.ContentView = 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;
}
});
ビュー/main.js
var AppRouter = Backbone.Router.extend({
routes: { "content/:type" : "contentType" },
contentType: function(type) {
var contentList = new ContentCollection({type : type});
contentList.fetch({success: function(){
$("#content").empty().append(new ContentListView({collection: contentList}).el);
}});
this.headerView.selectMenuItem('build-menu');
},
utils.loadTemplate([
'ContentView'
], function() {
app = new AppRouter();
Backbone.history.start(); });
contentView.html
name (<% tag won't print here)
ルート/modules.js
exports.findContentByType = function(req, res) {
var type = req.params.type;
db.collection('content', function(err, collection) {
collection.find({'type': type.toString()}).toArray(function(err, items) {
res.send(items);
});
});
};
サーバー.js
app.get('/content/:type', module.findContentByType);