Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think what you want, is to define <code>url</code> as a function, and have a <code>user</code> attribute on your collection:</p> <pre><code>var UniqueArticles = Backbone.Collection.extend({ model: Article, initialize: function(){ var self = this; myApp.vent.on("showarticles", function(someuser){ self.user = someuser; self.fetch(); } }, url : function(){ var fragment = '/api/visitingarticles/'; if(this.user &amp;&amp; this.user.id){ return fragment + this.user.id; } return fragment; } }); </code></pre> <p>(Disclaimer: untested code, but it works in my head :D)</p> <p>Then each time you trigger the event, the <code>user</code>attribute is updated, the collection is reset with the updated url.</p> <p>As a side note, you might want to look into using a filtered collection. I've implemented that idea in my book, based on Derick Bailey's code here: <a href="http://jsfiddle.net/derickbailey/7tvzF/" rel="nofollow">http://jsfiddle.net/derickbailey/7tvzF/</a></p> <p>Here is my version: <a href="https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/common.js" rel="nofollow">https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/common.js</a></p> <p>And an example of its use (lines 38-41): <a href="https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L38" rel="nofollow">https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L38</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload