Monday, 15 July 2013

Best way to manage synchronous server side data in AngularJS -



Best way to manage synchronous server side data in AngularJS -

i'm using oauth manage session users in angularjs app. angular app needs know info session user because used in $resource objects(to create rest calls /user/:user/projects). want user variable global , synchronous, making async phone call $resource or $http user adds boilerplate code needs session user(because has wait user info homecoming server).

right i'm using dirty hack resolves issue: in index.ejs file(notice user rendered server side ejs)

<script type="text/javascript"> var session_user = <%- user %>; </script>

and

$rootscope.user = session_user

the problem when unit test controller, of course of study session_user not defined.

i'm thinking maybe create service returns session_user variable, can utilize mock testing. other ideas? recommended aproach kind of problems?

it doesn't seem much of dirty hack actually. seem problematic if session user alter without reloading html server (and thereby getting new session user).

angular modules have constant function utilize sort of thing:

angular.module("mymodule", []) .constant("user", window.session_user);

and can treat user injectable value in controllers.

angularjs

No comments:

Post a Comment