davewasmer / ember-data-actions

Rails-style non-CRUD RESTful actions for Ember Data
Other
33 stars 7 forks source link

Default action to property name #2

Closed knownasilya closed 9 years ago

knownasilya commented 9 years ago

Example

export default DS.Model.extend(ResourceActionsMixin, {
  revoke: action()
});

Instead of duplication, e.g. revoke: action('revoke').

davewasmer commented 9 years ago

Great idea. I assume that just uses the key argument to the computed property function, right?

davewasmer commented 9 years ago

Actually, not sure how to do this - revoke is a method, not a computed property, so it wouldn't have access to the key.

knownasilya commented 9 years ago

Maybe look at how Ember.inject.service() works?

https://github.com/emberjs/ember.js/blob/v1.11.1/packages/ember-metal/lib/injected_property.js#L26

davewasmer commented 9 years ago

It seems like Ember.inject.* methods return an InjectedProperty, which is essentially a computed property whose getter looks something up on the container and returns that. For instance, if you say

var book = Ember.Object.extend({
  session: Ember.inject.service()
});

You can only access the session service via book.get('session'). book.session will return the InjectedProperty instance.

Our problem is that revoke is a function. Even if we register the function with the container (and use instantiate: false) so it could be looked up, we only get the function back if we use user.get('revoke'), meaning it can only be invoked via user.get('revoke')({ some: 'params' }).

The double parens isn't very elegant syntax, and you'll lose the calling context (i.e. this won't be user).

I'm not sure this kind of dynamic lookup is possible with a function value, unless I'm missing something.

knownasilya commented 9 years ago

You are probably right. Oh well :smile_cat: