V
V
vasyaPupkinRussia2016-08-31 15:49:06
Angular
vasyaPupkinRussia, 2016-08-31 15:49:06

What are the ways to create real data models in angularjs?

I use angularJS, I always just threw the data received from the server into the rootscope, which are needed in several places in the application. There was a time, I decided to issue normally. Since REST communication with the server, I took the restangular library (no questions about it, it works fine, although someone may suggest more interesting options). I would like to make models like on the backend, a separate class (factory, service) with methods for getting save, processing output for various places. Using the model according to the Singleton principle - initialize once, and use the data in different places, when changing in the model, so that it changes throughout the application where the model outputs data.
But I can't find an answer to my problem.
Code example, how I imagine the model works.

angular.module('Start').factory('User', function(Restangular, $rootScope) {
  var User = function() {
    then = this;
    return data = _resource.get('current').then(function(result){
    		console.log(result);
    		then.data = result;
    	});
  };
    var _resource = Restangular.all('users');
    var data;

    return User;
});

After in controller get data like
var res = User();
console.log(res .data);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-08-31
@AMar4enko

Use the router's resolve sections to delay rendering the page until the required data has finished loading.
Make it possible to subscribe to the data update event in the service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question