FlavorScape
FlavorScape

Reputation: 14289

How to use restful instead of query syntax for this $resource factory?

So I've got a $resource factory like so:

(function() {
    'use strict';
    app.factory('WidgetFactory', [ '$resource', function($resource) {
        return $resource('http://localhost:8282/widget/:widgetId', { widgetId : '@id'},{
                rate: {method:'POST', params:{applyRating:true}}
            });
    }]);
}());

Then in my controller I want to get the widget with id 1:

this._widgets = WidgetFactory.get({ id:1 }); // or WidgetFactory.query({ id:1 })

But my then the request goes out as http://localhost:8282/widget?id=1 instead of widget/1

How to formulate the request the $resource from the controller to produce widget/1? I guess my noob question is how do I pass-in '@id' parameter when using my resource factory (how does '@id' really work)?

Upvotes: 0

Views: 70

Answers (1)

Marc Kline
Marc Kline

Reputation: 9409

It could be best to match paramDefault key/value, for reasons just recently described in angularjs $resource not replacing variable in url template for POST

So, then if your $resource config looks like

app.factory('WidgetFactory', [ '$resource', function($resource) {
    return $resource('http://localhost:8282/widget/:widgetId', { widgetId : '@widgetId'},{
            rate: {method:'POST', params:{applyRating:true}}
        });
}]);

your controller call would look like

this._widgets = WidgetFactory.get({ widgetId:1 });

Demo

Upvotes: 1

Related Questions