0

I want the user to be able to set the slug name (URL) for a document in my app, but also I need some control so users don't override each other. It needs to be a separate call (not integrated with create/update) so the user can get visual feedback on their own slug name suggestions.

Therefore I've created a suggestSlug API call that takes an optional slug parameter as seed for the final slug name.

This is what my Express routes looks like:

app.get('/api/projects/suggestSlug/:slug', projects.suggestSlug);
app.get('/api/projects/suggestSlug', projects.suggestSlug);
app.get('/api/projects', projects.list);
app.get('/api/projects/:id', projects.show);

Now, I want to extend ngResource on the client side (AngularJS) to make use of this API:

angular.module('myapp.common').factory("projectModel", function ($resource) {
    return $resource(
        "/api/projects/:id",
        { id: "@id" },
        {
            update: { method: "PUT", params: { id: '@_id' } },
            del: { method: "DELETE", params: { id: '@_id' } }
        }
    );
});

How do I extend the ngResource client to use my new API?

Tom Söderlund
  • 4,743
  • 4
  • 45
  • 67

1 Answers1

0

This was my solution: adding a separate $http-based method to my projectModel:

angular.module('myapp.common').factory("projectModel", function ($resource, $http) {

    var projectModel = $resource(
        "/api/projects/:id",
        { id: "@id" },
        {
            update: { method: "PUT", params: { id: '@_id' } },
            del: { method: "DELETE", params: { id: '@_id' } }
        }
    );

    projectModel.suggestSlug = function (slugSuggestion, callback) {
        $http.get(
            '/api/projects/suggestSlug/' + slugSuggestion
            ).success(callback).error(function(error) {
                console.log('suggestSlug error:', error);
            });
    };

    return projectModel;
});
Tom Söderlund
  • 4,743
  • 4
  • 45
  • 67