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

Async methods should honor methods on a resource definition. #68

Closed
kentcdodds opened this issue May 30, 2014 · 3 comments
Closed

Async methods should honor methods on a resource definition. #68

kentcdodds opened this issue May 30, 2014 · 3 comments
Assignees

Comments

@kentcdodds
Copy link
Contributor

When I do this:

var User = DS.definitions['user'][DS.definitions['user'].class];
DS.findAll('user', {}).then(function(users) {
  console.log(users[0] instanceof User); // <-- logs "false"
  console.log(DS.get('user', 1) instanceof User); // <-- logs "true"
});

I would expect that the objects returned from findAll are also instances of the user. Here's how I'm using it:

// ... some state definitions

$stateProvider.state('root.auth.home.locks.list', {
  url: '',
  templateUrl: homeTemplateRoot + 'Locks/LocksCtrl.html',
  controller: 'LocksCtrl',
  resolve: {
    locks: resolveDS('findAll', ['lock', {}]),
    users: resolveDS('findAll', ['user', {}])
  }
});

// ... other state definitions

function resolveDS(method, args) {
  if (!_.isArray(args)) {
    args = [args];
  }
  return function(DS) {
    return DS[method].apply(DS, args);
  };
}

// in my LocksCtrl
angular.module('pk.web').controller('LocksCtrl', function ($scope, locks, users, _) {
  // some $scope initialization stuff using the injected users.
});

To be able to get the returned objects as an instance of a User, I would have to do DS.get for everything. If that's the way I'm supposed to be using this and I'm not supposed to actually inject the results of the call to findAll then that's fine. Though I would prefer to simply do it as I am because it seems to make sense to me this way.

@kentcdodds
Copy link
Contributor Author

This is related to #54

@jmdobry
Copy link
Member

jmdobry commented May 30, 2014

Should be fixed by changing this line to var injected = this.inject(resourceName, data); and this line to return injected;.

@kentcdodds
Copy link
Contributor Author

Seems legit

jmdobry added a commit that referenced this issue May 31, 2014
@jmdobry jmdobry self-assigned this May 31, 2014
@jmdobry jmdobry closed this as completed May 31, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants