Reputation: 7855
I have the following $resource factory:
app.factory "FeatureKey", ($resource) ->
$resource "/api/v1/feature_keys/:id", { id: "@id" }
In a directive, I use it like so:
@search = (term) ->
FeatureKey.query(name: term).$promise.then (keys) =>
if keys.length > 0
@keys = keys
else
@keys = [{id: 0, name: term}]
@current.key = @keys[0]
What I'd like to is move most of the logic into the $resource factory so that my directive method would look like so:
@search = (term) ->
FeatureKey.search(term).$promise.then (keys) =>
@keys = keys
@current.key = @keys[0]
Upvotes: 0
Views: 43
Reputation: 101730
You just need to have your factory return an object with a search
method instead of returning a resource:
app.factory "FeatureKey", ($resource) ->
r = $resource "/api/v1/feature_keys/:id", { id: "@id" }
search: (term) ->
r.query(name: term).$promise.then (keys) ->
if keys.length then keys else [{id: 0, name: term}]
Then you can use it like this:
FeatureKey.search(term).then (keys) =>
@keys = keys
@current.key = @keys[0]
Upvotes: 1