Writing an Angular Factory Using the CoffeeScript Class Style

Recently I am trying to learn AngularJs and CoffeeScript by writing a small application.
After reading some blogs, I can write an Angular controller and a service with the CoffeeScript Class. The following is an example of a controller.

libr = angular.module('libr.controllers.main', [])
class MainController
  @$inject: ['$scope']
  constructor: (@$scope) ->
    @$scope.test = test
  test: ()->
    console.log 'Hello'
libr.controller 'MainCtrl', MainController

And it works well.

But I can not convert Angular factory so successfully using the Coffee Class style.

var app = angular.module('app', ['ngResource', 'ngRoute']);

// Some APIs expect a PUT request in the format URL/object/ID
// Here we are creating an 'update' method 
app.factory('Notes', ['$resource', function($resource) {
  return $resource('/notes/:id', null,
      {
          'update': { method:'PUT' }
      });
}]);

This code is an example of code on the official AngularJs website: http://docs.angularjs.org/api/ngResource.$resource

Can someone help me convert it to a CoffeeScript class style?

+4
source share
2

factory , , .

app = angular.module("app", ["ngResource", "ngRoute"])

app.factory "Notes", ["$resource", ($resource) ->
    $resource("/notes/:id", null, { update: { method: "PUT" } })
]

, , coffeescript ( ), - :

class Foo
  constructor: ($someDependency) ->
    @myVar = "hello"

  bar: ->
    console.log @myVar

app.service("Foo", ['$someDependency', Foo])
+3
class Notes
  url = "/notes/:id"
  constructor:(@$resource) ->
    return $resource url, {id: "@id"}, { update: {method: "PUT"} } 


angular.module("app").factory "Notes", ["$resource", Notes]
+2

Source: https://habr.com/ru/post/1526505/


All Articles