javascript - Backbone Sub-Collections & Resources -
i'm trying figure out collection/model system can handle retrieving data given context it's asked from, example:
available "root" resources:
/api/accounts /api/datacenters /api/networks /api/servers /api/volumes
available "sub" resources:
/api/accounts/:id /api/accounts/:id/datacenters /api/accounts/:id/datacenters/:id/networks /api/accounts/:id/datacenters/:id/networks/:id/servers /api/accounts/:id/datacenters/:id/networks/:id/servers/:id/volumes /api/accounts/:id/networks /api/accounts/:id/networks/:id/servers /api/accounts/:id/networks/:id/servers/:id/volumes /api/accounts/:id/servers /api/accounts/:id/servers/:id/volumes /api/accounts/:id/volumes
then, given collection/model system, able things like:
// first account var account = accountcollection.fetch().first() // datacenters associated account account.get('datacenters') // servers associated first datacenter's first network account.get('datacenters').first().get('networks').first().get('servers')
not sure if makes sense, let me know if need clarify anything.
the biggest kicker why want able this, if request being made (ie account.get('datacenters').first().get('networks')
) hasn't made (the networks of datacenter aren't loaded on client) made (or can fetch()
d perhaps?)
any can give appreciated!
you can pass options fetch translated querystring params.
for example:
// first account var account = accountcollection.fetch({data: {pagesize: 1, sort: "date_desc"}});
would translate to:
/api/accounts?pagesize=1&sort=date_desc
it not quite fluent dsl expressive , efficient since transmits objects requested rather filtering post fetch.
edit:
you can lazy load sub collections , use same fetch params technique filter down list query string criteria:
var account = backbone.model.extend({ initialize: function() { this.datacenters = new datacenters; this.datacenters.url = "/api/account/" + this.id + '/datacenters'; } });
then account instance:
account.datacenters.fetch({data: {...}});
backbone docs on fetching nested models , collections
Comments
Post a Comment