2011-07-18 14 views
6

URLに使用するためにコレクションにIDを渡す必要がありますが(例:/user/1234/projects.json)、これを行う方法がわからない場合は、例があります素晴らしい。バックボーンセットコレクション属性(URL用)

私のアプリケーションの構造は「ユーザー」のコレクションが引き出されてレンダリングされるので、ユーザーが「ドキュメント」をクリックしてサーバーから新しいコレクションにプルして新しいビュー。問題は、ドキュメントコレクションにユーザーIDを渡して、documents.fetch()の関連URLを提供することです。

//in the the view initialize function  
    this.collection = new Docs(); 
    this.collection.project_id = this.options.project_id; 
    this.collection.fetch(); 

    //in the collection 
    url: function() { 
    return '/project/api/' +this.project_id+'/docs'; 
    } 
+0

が私のために働きました。ありがとう! –

答えて

7

あなたのユーザーコレクションのURLが/ユーザーに設定する必要があります。


は、ここでは一例であり、私はそれを持っていると思います。それが設定されると、あなたのモデルはその魔法を実行するためにそのURLを利用するべきです。モデルがコレクション内にある場合、 'url'メソッドを呼び出すと/ user /:idが返されると私は(完全に肯定的ではない)と信じています。だからあなたの典型的なREST-ish機能はすべて '/ user /:id'で利用されます。あなたが関係を持って何かをしようとしているなら(ユーザーは多くの文書を持っています)、それは一種のすすぎと繰り返しです。したがって、あなたのドキュメントコレクション(正しいユーザーに似ていますか?)では、urlに 'user_instance.url/documents'を設定します。

は(urlRootのためのバックボーン0.5.1へのアップグレード)、あなたはこのような何かをしたい基幹モデルとの一対多の関係を表示するには:

var User = Backbone.Model.extend({ 
    initialize: function() { 
     // note, you are passing the function url. This is important if you are 
     // creating a new user that's not been sync'd to the server yet. If you 
     // did something like: {user_url: this.url()} it wouldn't contain the id 
     // yet... and any sync through docs would fail... even if you sync'd the 
     // user model! 
     this.docs = new Docs([], {user_url: this.url}); 
    }, 
    urlRoot: '/user' 
}); 

var Doc = Backbone.Model.extend(); 

var Docs = Backbone.Collection.extend({ 
    initialize: function(models, args) { 
     this.url = function() { args.user_url() + '/documents'; }; 
    } 
}); 

var user = new User([{id: 1234}]); 
user.docs.fetch({ success: function() { alert('win') }); 
+0

ありがとう、それは1対多です。私が明らかにしていないのは、文書コレクションに 'user_instance'を取得する方法です(私は主な質問に例を追加しました)。 –

+0

私は今、理解していると思います。 –

+0

2年後、これはまだ関連しており、モデルURLを分離する非常に良い方法です。私は特に、Docsモデルが/ドキュメントをURLに追加する責任があるのが好きです。 Backbone FAQは、これがUserモデルの責任であることを示唆していますが、私は同意しません。この方法ではIMOはよりクリーンです。 – fiznool

5

は、なぜあなたはURLプロパティをオーバーライドする必要があります機能付きのコレクションの..あなたは何ができる?

this.collection = new Docs(); 
this.collection.project_id = this.options.project_id; 
this.collection.url = '/project/api/' + this.options.project_id + '/docs'; 
this.collection.fetch(); 
+0

これが最適なソリューションです。 – SeanK

0

私はクレイグ・モンソンから答えを好きですが、それは、私は二つのことを修正するために必要な作業を取得する:

  • ドキュメント

にURL関数からドキュメント

  • に更新例をreturn文を渡す前に、ユーザーのURL方式を結合:

    var User = Backbone.Model.extend({ 
        initialize: function() { 
         // note, you are passing the function url. This is important if you are 
         // creating a new user that's not been sync'd to the server yet. If you 
         // did something like: {user_url: this.url()} it wouldn't contain the id 
         // yet... and any sync through docs would fail... even if you sync'd the 
         // user model! 
         this.docs = new Docs([], { user_url: this.url.bind(this) }); 
         }, 
         urlRoot: '/user' 
        }); 
    
    var Doc = Backbone.Model.extend(); 
    
    var Docs = Backbone.Collection.extend({ 
        initialize: function(models, args) { 
         this.url = function() { return args.user_url() + '/documents'; }; 
        } 
    }); 
    
    var user = new User([{id: 1234}]); 
    user.docs.fetch({ success: function() { alert('win') });