angularjs-resource

How can I extend the constructor of an AngularJS resource ($resource)?

对着背影说爱祢 提交于 2019-11-28 19:15:56
问题 I have a model, defined using $resource , that I am successfully loading. Each loaded instance is, as promised, an instance of the class I defined. (The example below is from the Angular docs. In it, User.get results in an object that is an instanceof User .) var User = $resource('/user/:userId', {userId:'@id'}); However, imagine each User comes over the wire like this: { "username": "Bob", "preferences": [ { "id": 1, "title": "foo", "value": false } ] } I defined a Preference factory that

TypeError: undefined is not a function in Angular Resource

独自空忆成欢 提交于 2019-11-28 12:24:35
When trying to poll a custom method copies on an AngularJS Resource I get the following error at angular.js:10033 : (The method copy works just fine.) TypeError: undefined is not a function at https://code.angularjs.org/1.3.0-beta.8/angular-resource.min.js:9:347 at Array.forEach (native) at q (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:7:280) at q.then.p.$resolved (https://code.angularjs.org/1.3.0-beta.8/angular-resource.min.js:9:329) at J (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:101:5) at J (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:101:5) at https:/

'Best' practice for restful POST response

六眼飞鱼酱① 提交于 2019-11-28 02:38:49
So nothing new here I am just trying to get some clarification and cannot seem to find any in other posts. I am creating a new resource restulfully, say: /books (POST) with a body: { title: 'The Lion, the Witch and the Wardrobe', author: 'C. S. Lewis' } I know that I should return a 201 (Created) with a Location header of the new resource: Location: /books/12345 The question I cannot seem to answer for myself is what should the server return in the body. I have often done this type of response: { id: 12345, title: 'The Lion, the Witch and the Wardrobe', author: 'C. S. Lewis' } I have done this

Angular - extending $resource subobject with custom methods

时光怂恿深爱的人放手 提交于 2019-11-27 10:41:20
In most cases the result of <custom-resource>.query() method is an array, which can be easily extended with some methods (business logics) with the following (factory) code: var Data = $resource('http://..'); Data.prototype.foo = function() {return ...}; this is perfect for using with ng-repeat / ng-class, like so: <tr ng-repeat="item in responseData" ng-class="{warning: item.foo()}">..</tr> My problem is that every list response is encapsulated in an object which, besides the actual list, has some meta-properties (sorting info etc), so the final object returned is like this: { order_field:

TypeError: undefined is not a function in Angular Resource

和自甴很熟 提交于 2019-11-27 07:01:04
问题 When trying to poll a custom method copies on an AngularJS Resource I get the following error at angular.js:10033 : (The method copy works just fine.) TypeError: undefined is not a function at https://code.angularjs.org/1.3.0-beta.8/angular-resource.min.js:9:347 at Array.forEach (native) at q (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:7:280) at q.then.p.$resolved (https://code.angularjs.org/1.3.0-beta.8/angular-resource.min.js:9:329) at J (https://code.angularjs.org/1.3.0-beta.8

How to refresh / invalidate $resource cache in AngularJS

岁酱吖の 提交于 2019-11-27 02:56:55
I have a simple User $resource that uses the default $http cache implementation like so: factory('User', function($resource){ return $resource(endpoint + '/user/current/:projectId', {}, {get: { cache: true, method: 'GET' } } ); }) This works very well, i.e. my server is only called once in my application, then the value is fetched from cache. But I need to refresh the value from the server after a certain operation. Is there an easy way to do that? Thanks. Anthonny Keep the boolean and get the $http cache: var $httpDefaultCache = $cacheFactory.get('$http'); Then you can control it like any

Protractor times out waiting for sync with page when using $resource

痞子三分冷 提交于 2019-11-27 02:13:17
问题 I'm testing Protractor with a small AngularJS app. This is the test: describe('Testing Protractor', function() { var draftList; it('should count the number of drafts', function() { browser.get('#/'); draftList = element.all(by.repeater('newsletter in drafts')); expect(draftList.count()).toEqual(2); }); }); Controller: angular.module('myApp.controllers', []). controller('DraftsCtrl', ['$scope', 'Draft', function($scope, Draft) { $scope.drafts = Draft.query(); }]) Draft service: angular.module(

add a custom function on angular $resource [duplicate]

微笑、不失礼 提交于 2019-11-27 01:49:22
问题 This question already has answers here : Angular - extending $resource subobject with custom methods (4 answers) Closed 4 years ago . I have an angular resource that goes something like this app.factory('User', function ($resource) { return $resource( '/api/user/:listCtrl:id/:docCtrl/', { id: '@id', listCtrl: '@listCtrl', docCtrl: '@docCtrl' }, { update: { method: 'PUT' }, current: { method: 'GET', params: { listCtrl: 'current' } }, nearby: { method: 'GET', params: { docCtrl: 'nearby' },

'Best' practice for restful POST response

ε祈祈猫儿з 提交于 2019-11-26 23:46:30
问题 So nothing new here I am just trying to get some clarification and cannot seem to find any in other posts. I am creating a new resource restulfully, say: /books (POST) with a body: { title: 'The Lion, the Witch and the Wardrobe', author: 'C. S. Lewis' } I know that I should return a 201 (Created) with a Location header of the new resource: Location: /books/12345 The question I cannot seem to answer for myself is what should the server return in the body. I have often done this type of

Angular - extending $resource subobject with custom methods

醉酒当歌 提交于 2019-11-26 17:57:40
问题 In most cases the result of <custom-resource>.query() method is an array, which can be easily extended with some methods (business logics) with the following (factory) code: var Data = $resource('http://..'); Data.prototype.foo = function() {return ...}; this is perfect for using with ng-repeat / ng-class, like so: <tr ng-repeat="item in responseData" ng-class="{warning: item.foo()}">..</tr> My problem is that every list response is encapsulated in an object which, besides the actual list,