[Owncloud] Routing for Dummies and best practices

Bart Visscher bartv at thisnet.nl
Thu Mar 21 09:21:20 UTC 2013


On Thu, Mar 21, 2013 at 02:01:49AM +0100, Thomas Tanghus wrote:
> I could probably go the way and use the app framework, but I like to know what's happening behind
> the curtains and try out simple implementations before doing that. My limited experience tells me it
> pays out in the long run.
> Nothing of this is tested btw, purely hypothetical.
> 
> So, in contacts app I want to get the contacts from a  specific address book. I set up a route akin to:
> 
> (say 'backend' == 'local' and 'id' == '123')
> 
> $this->create('contacts_address_book_collection', 'contacts/addressbook/{backend}/{id}/contacts')
> 	->get()
> 	->action(
> 		function($params){
> 			$app = new App();
> 			$addressBooks = App->getAddressBook($params['backend'], $params['id']);
> 		}
> 	);
> 
> So in my client side script I can call this by:
> 
> 	var url = OC.Router.generate('contacts_address_book_collection', {backend: backend, id: id});
> 	$.getJSON(url, function(jsondata) {
> 		//
> 	});
> 
> And this would translate into:
> 
> 	/index.php/apps/contacts/addressbook/local/123/contacts   ?
> 
> Am I right so far or have I totally misunderstood it?

Yes, this is how to use the routing system.

> 
> So can I add more parameters to the OC.Router.generate() call and have them available in $params
> or will they have to be in the URI?

You can add more parameters, these will be included added as query
parameters, and will be available in $_GET

> 
> And what would be most efficient: Using OC.Router.generate() or requesting the URI directly?

requesting the URI directly would be more efficient, BUT you then need
to change it also here when the url changes. Using OC.Router.generate()
you only need to change it in one place.

> 
> On a side note http://doc.owncloud.org/server/5.0/developer_manual/app/app/routes.html 
> mentions OC.Router.registerLoadedCallback()
> Would this be the proper way to use it:
> 
> $(document).ready(function() {
> 	OC.Router.registerLoadedCallback(function() {
> 		OC.Contacts.init();
> 	});
> });

If you use OC.Router.generate() in OC.Contacts.init() then this is the
way to go. The routes are loaded async, so it is possible they are not
available on document ready.

Bart



More information about the Owncloud mailing list