1

バックボーン、エクスプレス、および 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);
4

1 に答える 1

1

ここにはいくつかの問題があります。

  1. this.headerView.selectMenuItem('build-menu');headerView(ルーター内)は、ルーター オブジェクトで定義したことを意味しますが、定義されていません。
  2. 同様に、this.template内部ContentViewは定義されていません

#1の行を削除し、ダミーのテンプレートを定義するとContentView

template: _.template("<div> Test: <%= version %> </div>"),

次に、少なくともビューがレンダリングされます -ここを参照してください。(これはダミー データです。サーバーが有効な/期待される JSON を返していることを確認できません。)

于 2012-12-22T07:16:03.607 に答える