Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about namespaces #1

Open
coniel opened this issue Aug 6, 2015 · 2 comments
Open

Question about namespaces #1

coniel opened this issue Aug 6, 2015 · 2 comments
Labels

Comments

@coniel
Copy link

coniel commented Aug 6, 2015

Thanks a lot for the package, I just have a couple of questions about the namespaces defined in app:lib's core.js:

What do you use App.controllers for?
What is the Secondary namespace for?

@coopermaruyama
Copy link
Owner

I put RouteControllers in App.controllers. example:

// assumes you've already defined an AppController somewhere else.
/*****************************************************************************/
/* Routes */
/*****************************************************************************/
var AppController = App.controllers.AppController;

/**
 * All products
 */
App.controllers.products_index = AppController.extend({
  pageTitle: 'All products',
  name: "products.index",
  template:"items_grid",
  waitOn:function(){
    return Meteor.subscribe('allProducts');
  },
  data:function(){
     return {
       items: Products.find({}, {sort: {model: 1} }),
       filters: []
     }
  }
});

/**
 * View product.
 */
App.controllers.show_product = AppController.extend({
  name: 'products.show',
  template: 'item_show_details',
  pageTitle: 'Product Details',
  disableMeta: true,
  collection: 'products',
  fastRender: true
});

Router.route("/products", {controller: Products.controllers.index});
Router.route("/products/new", {controller: Products.controllers.index, filter: [{new: true}]});
Router.route('/products/:slug', {controller: Products.controllers.show});

As you can see, controllers are used just like routes except you can have multiple routes share the same controller, so it allows you to create 'groups'; Controllers can inherit other controllers, so if you defined an onBeforeAction callback on AppController, they will be run for these controllers as well.

Oh, and in case you didn't notice from that last sentence: when you extend a controller and define a callback that was already defined by the controller you extend, you don't overwrite it - you append to it. I believe routers don't have this behavior, so in certain situations controllers are handy. Last, notice how I defined some arbitrary keys on the controllers (e.g. pageTitle). This is because you can access controllers in your template's helpers/events/callbacks like this:

var ctor = Iron.controller();
var pageTitle = ctor.pageTitle;
Session.set('pageTitle', pageTitle);

Hopefully that illustrates when they could be useful.

As for the secondary namespace - it's just showing that if you want, you can define more namespaces; It's totally up to you.

Let me know if that clears things up. I'm going to keep this open as a reminder to clarify a few things in the repo.

@coniel
Copy link
Author

coniel commented Aug 6, 2015

Thanks for the explanation. I had a suspicion it was for route controllers. I actually use a similar pattern, but with Flow Router (using route groups).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants