M
M
microf2015-10-19 20:28:29
JavaScript
microf, 2015-10-19 20:28:29

Can you tell me about promise and $resource?

Something I don't understand at all.
There is data in json file. Well, or in the base, not the point ..

[{
  "id" : "34234234234234234",
  "name" : "Peter"
},
  {
    "id" : "566464645643643",
    "name" : "Max"
  }]

I want to pick them up and, accordingly, process them. I'm doing a service
(function () {
    'use strict';

    angular
        .module('data')
        .service('dataService', dataService);

    dataService.$inject = ['$resource'];
    function dataService($resource) {
        this.getData = getData;

        var resource = $resource('server/data.json');
        ////////////////

        function getData() {
            return resource.query();
        }
    }
})();

In the controller I take
function DataListController(dataService) {
        var vm = this;
        vm.data = dataService.getData();
    }

and with the help of ng-repeat I display the values. Everything is fine, the names are displayed as expected. But what does this mean?
0be9e5b3a69f4fcdab6abda6182b5085.png
Well, that is. which promise has 0:Resource and 1:Resource with my data
Why is this needed at all? I read about promises - well, yes, well, you can either resolve or reject, but why?
And is it possible to iterate over this "object" using all sorts of forEach and other loops? So I want to get an array of all the first letters of the names in my database - how to do this?
I read this article , but right at the beginning "... Once a promise is resolved or rejected, its state can no longer change, which ensures that the state remains unchanged for any number of checks. Which does not mean that at different stages of checks you will get one and also meaning.
This phrase is not available to my logic - it cannot change, but this does not mean that the values ​​will be the same.
Is it possible to simply get what is in the database (json file) {'id':'werwer','name':'Max'}and work with it? What is cymus?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arthur, 2015-10-19
@microf

The first is to convert an Angular object (which has all sorts of one- and two-dollar parameters, all these $xxx and $$xxx ) into a normal one (so-called raw json ) you need to use standard Angular methods out of the box:

angular.fromJson(response)
и
angular.toJson(response)

Just tinker with these functions and see what they return.
About promises. When you call any $resource -method, for example, .query() , then immediately, instantly, an object is returned to you with two parameters - $promise and $resolved: false . After the resource has received the data, a bunch more data is added to your object with two parameters (well, the json data that the server returned to you), and the $resolved parameter becomes true .
So $promise is needed when you need to do something after loading the data :) After all, when you sent a request, you don’t know when the answer will come, but $promiseknows and allows you to do something just after loading. You need to refer to a promise like this:
vm.data = dataService.getData().$promise.then(success, error);

function success(resource) {
    // тут не данные в resource, а объект $resource с данными, тут можно почистить его через angular.fromJson и т.п.
}

Here, after receiving the data, your vm.data is immediately replenished with the response data (and you display something there through ng-repeat - Angular there understands that you need to display only your data, and does not display two more parameters $promise and $ resolved , although they are present in the object, as you have already displayed in the console.log and made sure), and you can do something else in the success function.
You can also immediately not assign anything to vm.data , but first do some actions in success and only then assign it, like this:
dataService.getData().$promise.then(success, error);

function success(resource) {
    // тут не данные в resource, а объект $resource с данными, тут можно почистить его через angular.fromJson и т.п.
    // сначала что-нибудь делаем с полученными данными, а потом присваиваем их в скоуп
    vm.data = transformMyData(resource);
}

S
Sergey, 2015-10-19
Protko @Fesor

read the normal strategy about promises

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question