3

バックボーン コレクションを使用してデータベースにクエリを実行する必要があります。これを行う方法がわかりません。どこかにURLを設定する必要があると思いますが、それがどこにあるのかわかりません。これは非常に基本的な質問に違いないことをお詫びしますが、CodeSchool.com でバックボーン コースを受講しましたが、どこから始めればよいかまだわかりません。

これは私がコレクション用に持っているコードです:

var NewCollection = Backbone.Collection.extend({

    //INITIALIZE

    initialize: function(){

        _.bindAll(this); 

        // Bind global events

        global_event_hub.bind('refresh_collection', this.on_request_refresh_collection);

    } 

    // On refresh collection event

    on_request_refresh_collection: function(query_args){

        // This is where I am lost. I do not know how to take the "query_args"

        //   and use them to query the server and refresh the collection <------

    } 

})
4

1 に答える 1

4

簡単な答えは、次のようにBackbone.CollectionにURLプロパティまたは関数を定義することです。

initialize: function() {
    // Code
},
on_request_refresh_collection: function() {
    // Code
},
url: 'myURL/whateverItIs'

また

url: function() {
    return 'moreComplex/' + whateverID + '/orWhatever/' + youWant;
}

URL関数が定義されたらfetch()、そのコレクションインスタンスでを実行するだけで、URLを設定したものがすべて使用されます。

編集-------コレクションクエリの作成

fetch()したがって、URLを設定すると、ネイティブメソッドを使用して簡単にクエリを実行できます。

fetch()data:{}次のようにクエリ引数をサーバーに送信できる場所と呼ばれるオプションを取ります。

userCollection.fetch({
    data: {
        queryTerms: arrayOfTerms[],  // Or whatever you want to send
        page: userCollection.page,  // Pagination data
        length: userCollection.length  // How many per page data
        // The above are all just examples. You can make up your own data.properties
    },
    success: function() {
    },
    error: function() {
    }
});

次に、サーバー側で、リクエストと出来上がりのパラメーターを取得するようにします。

于 2012-08-24T19:44:30.457 に答える