1

I have a ListRoute that can be used standalone or overridden with a filter method.

Currently the overriding route accesses a global variable. I'm trying to get rid of globals but don't see a way to pass the context along to the function.

How can I pass the context along (in this case the 'currentUser') without globals. Or how should I rewrite this.

Why is 'this' in the extending function the window? I was hoping I'd be able to at least call this.modelFor() to access the necessary information.

Encompass.WorkspacesListRoute = Ember.Route.extend({

  controllerName: 'workspaces.list',
  templateName:   'workspaces/list',
  filter: function(workspace) {
    return true; //return everything by default, extenders will override this
  },

  model: function() {
    var store = this.get('store');
    return store.filter('workspace', this.get('filter'));
  },

});

Encompass.WorkspacesMineRoute = Encompass.WorkspacesListRoute.extend({

  filter: function(workspace) {
    //at this point 'this' is the window (why not the route?)
    return (Encompass.get('currentUser') === workspace.get('owner'));
  }

});
Amir T
  • 2,560
  • 16
  • 20

2 Answers2

0

From a pure javascript perspective this helped me understand the scope and context issues a bit better: http://ryanmorr.com/understanding-scope-and-context-in-javascript/

Calling this.get('filter').bind(this) seems to work.

return store.filter('workspace', this.filter.bind(this));

Whereas .call(this) drops the argument.

Community
  • 1
  • 1
Amir T
  • 2,560
  • 16
  • 20
0

I believe calling the getter takes it out of the scope in terms of a functional scope. You should be able to do it by just passing in this.filter.

http://emberjs.jsbin.com/lunitoyi/1/edit

Kingpin2k
  • 47,007
  • 10
  • 75
  • 96
  • Thanks, I don't know why I keep calling the getter. It seems to work in the jsbin however when I try `this.filter` as a param to `store.filter()`, `this` winds up being the window again. So I suppose store.filter is dropping the scope also...? I was able to use `bind` successfully. – Amir T Jun 26 '14 at 20:41