I want to start preparing for Angular 2, converting existing code to ES6 (as recommended in this video ).
However, I am at an impasse right away or maybe I donβt know how to proceed. In the video, they show the transformation of the service. In my code, I am trying to convert a factory that is similar, but actually quite different when trying to convert to ES6. The service follows the method of instantiating the class easily, but factories should return the entered object.
My code started like this:
melange.factory('SomeService', ['$resource', function ($resource) { var someResource = $resource('/api/endpoint'); someResource.customQuery = function() {
My first unsuccessful attempt. So I immediately started converting to ES6 and came up with the following:
// notice I changed this to service instead of factory melange.service('SomeService', ['$resource', SomeService]); class SomeService { constructor ($resource) { var someResource = $resource('/api/endpoint'); someResource.customQuery = function() { // ... do some custom stuff }; return someResource; } }
But this is not so ... the constructor returns the resource.
Maybe an attempt at success is really a resource (or indeed a Route ), which is what I want "class-ify". But since the Resource object has a certain interface already from the methods, I will need my class to extend the base resource. But this is generated dynamically by calling the $ resource factory function. So I came up with this possibly correct code:
melange.service('SomeService', ['$resource', SomeResource]); var $resource = angular.injector().get('$resource'); class SomeResource extend $resource('/api/endpoint') { customQuery() { // ... do some custom stuff } }
Thus, I must get $ resource from the injector before declaring the class. I'm just not sure if the $ resource extension ('/ api / endpoint') is even true with ES6. It seems to work at all during babel forwarding, though .
Am I doing it right?
angularjs ecmascript-6
Tim Kindberg Mar 11 '15 at 16:32 2015-03-11 16:32
source share