angularjs - How can I refactor this angular directive with a custom $resource factory method? -
i have following $resource factory:
app.factory "featurekey", ($resource) -> $resource "/api/v1/feature_keys/:id", { id: "@id" }
in directive, use 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 move of logic $resource factory directive method so:
@search = (term) -> featurekey.search(term).$promise.then (keys) => @keys = keys @current.key = @keys[0]
you need have factory return object search
method instead of returning 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 keys else [{id: 0, name: term}]
then can use this:
featurekey.search(term).then (keys) => @keys = keys @current.key = @keys[0]
Comments
Post a Comment