Is this architecture valid in Angular 1.2 and above

There are many sample code that uses $ resource. I came across this and the code is pretty clear: https://github.com/apotry/stockwatch

I like this example because:

  • It interacts with the Rails backend since I use Angular
  • It uses $ resource
  • It uses a route without a trace (ohlc)
  • The code is pretty clean

Calling certain functions is quite simple, as shown in the controller code below, but is it recommended to embed a save in a factory?

My question is : now that Angular 1.2+ includes promises, is this type of code still valid and considered good practice? How does this code respond to an error condition?

Resources are defined here.

app.factory('Stock', ['$resource', function($resource) {
  function Stock() {
    this.service = $resource('/api/stocks/:stockId', {stockId: '@id'}, {'update': { method: 'PUT' }});
  };
  Stock.prototype.all = function() {
    return this.service.query();
  };
  Stock.prototype.delete = function(stId) {
    return this.service.remove({stockId: stId});
  };
  Stock.prototype.create = function(attr) {
    return this.service.save(attr);
  };
  Stock.prototype.update = function(attr) {
    return this.service.update(attr);
  };
  Stock.prototype.ohlc = function(stId) {
    return $resource('/api/stocks/:stockId/ohlc', {stockId: '@id'}).get({stockId: stId});
  }
  return new Stock; 
}]);

, (ohlc):

  $scope.requestOHLC = function (stockid) {
    return Stock.ohlc(stockid);
  }

  $scope.createStock = function() {
    $scope.fetchYahooFinanceData($filter('uppercase')    ($scope.newCompany["symbol"])).then(function(result) {
      $scope.error = false;
      $scope.stocks.push(Stock.create(result));
      $scope.newCompany = '';
    }, function(error) {
      $scope.error = true;
    });
  };

  $scope.deleteStock = function(id, idx) {
    Stock.delete(id);
    $scope.stocks.splice(idx, 1);
  };

$rest angular.

, . , , factory. REST ( rails), . : createPanelHeader scope $scope.selector.paneldata.primer3_parameter_id. , .

, , $prom.then, . ?

// Section: Create Panel header
createPrimer3Parameter = function() {
    primer3_parameter = Primer3Parameter.create().$promise.then(function(primer3_parameter){
    $scope.selector.paneldata.primer3_parameter_id = primer3_parameter.id;
    createPanelHeader();
    }, function() {
      alert('Error creating primer3parameter');
    })
};

.

REST Rails API 1 . , - , .

, , $resource, 1.2. raw $http Restangular.

, , 1.2 , Restangular. :

https://github.com/mgonto/restangular#using-values-directly-in-templates

UPDATE

100%, Bounty: https://bountify.co/write-an-angular-service-for-these-rails-routes-using-restangular

+4
2

- ?

, 1.2.0-rc3. angular 1.2 1.3 1.3.0-beta10, .

$scope.stocks.push(Stock.create(result));

$scope. index.html.erb stock:

<li ng-repeat="stock in stocks">
      <div id='symbol'>
        {{stock.symbol}}
      </div>

promises .

?

:

}, function(error) {
  $scope.error = true;
});

:

<div ng-show="error">
    There was a problem with the Yahoo Finance API. Please try again later.
</div>

, angular.

?

javascript sharewatch. . , Restangular . stockModel factory, Restangular, , (model). , , .

.factory('stocksModel', function (Restangular) {
  var model = {};
  var rest_stocks = Restangular.all('stocks');

  model.doSomethingRESTful = function (...) {
    // return a promise in case the controller needs it
    return rest_carts.some_restangular_method(...)
      .then(function() {
        model.some_data_which_was_acquired_RESTfully = ...;
      });
  };

  return model;
});

:

$scope.stocks = stocksModel;

:

{{stocks.some_data_which_was_acquired_RESTfully}}
+7

. , , Angular. , .,.

?

Angular - , . Angular , , . Angular , ( !) , .

, , , Angular, . , , .

Rails Angular,

, , Rails Angular. . , coffeescript, , , . , Rails, Angular.

* app/assets/javascripts
   * app
       * project_name_app.js.coffee.erb 
       * controllers
           * controllers.js 
       * directives 
           * directives.js
       * filters    
           * filters.js
       * resources  
           * resources.js
       * services
           * services.js

Rails , application.js include:

//= require app/project_name_app

app/project_name_app.js.coffee.erb

#= require_self
#= require app/controllers/controllers
#= require app/directives/directives
#= require app/filters/filters
#= require app/resources/resources
#= require app/services/services

, js (controllers.js, directives.js, filters.js, resources.js, services.js) :

//= require_tree .

project_name_app.js.coffee.erb, Angular, . , , .. - javascript, , require_tree.

$

$resources RESTFUL. , URL- , http . , URL-, , , . users_resource.js.coffee:

angular.module("DeployerApp.resources").factory "Users", ($resource) ->
  $resource "/users.json", {},
    index:
      method: "GET"
      isArray: true
      type: "User"


angular.module("DeployerApp.resources").factory "User", ($resource) ->
  $resource "/users/:user_id.json", {},
    show:
      method: "GET"
    update:
      method: "PUT"

angular.module("DeployerApp.resources").factory "CurrentUser", ($resource) ->
  $resource "/users/current.json", {},
    show:
      method: "GET"

, Users.index(). , User.show( user_id: 1 ). , I, , CurrentUser.show().

, . User $ .

Angular

$promise, . , $, :

CurrentUser.show success = (user) ->
    $scope.currentUser = user
, error = (data,status) ->
    # redirect to login

, coffeescript, .

$promise.then $, .

+2

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


All Articles