ruby on rails - Getting user data from server in angular controller without exposing data -


i've been trying user data rails controller angular controller:

in application.html.erb, define user so:

angular.element(document).ready(function() {     angular.module('mymodule').value('user', <%= @user.to_json.html_safe %>); }); 

in angular controller:

mymodule.controller('mycontroller', ['$scope', 'user', function($scope, user) {     $scope.user = user; }]); 

the problem exposes user data view. there better way this?

i've tried approach:

in module, created factory $resource object:

mymodule.factory('user', function($resource) {     return $resource('/users/:action', {},     {         'getuser': { method: 'get', params: { action: 'fetch_user.json' }, isarray:true }     });     }); 

in controller:

mymodule.controller('mycontroller', ['$scope', 'user', function($scope, user) {     $scope.user = user.getuser();      // tried following.     // throw undefined errors in places trying access $scope.user     user.getuser({}, function(data) {         $scope.user = data;     }); }]); 

however, either not load user data on time , throwing undefined errors throughout application or put every character of result in it's own array container.

the @user variable being returned json server in users_controller.rb ideally, want retrieve , store in $scope.user. problem requires username passed parameter retrieve correct user's data.

how can set $scope.user correctly , cleanly without exposing data?

directive

mymodule.factory('user', function($http) {     return {         getuser: function () {             // deferred object             var deferred = $q.defer();             $http.get('/fetch_user.json') {                 .success(function (data, status) {                      deferred.resolve(data);                 })                 .error(function (data, status) {                      deferred.reject("an error occured while trying users");                 });             }             return deferred.promise;        }     } } 

contoller:

mymodule.controller('mycontroller', ['$scope', 'user', function($scope, user) {      user.getuser().then(function (data) {          $scope.user = data;      }); }]); 

i'm assuming /fetch_user.json url, if not replace it.

update


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -