Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Plavski
Feb 1, 2006

I could be a revolutionary
I thought Mongo was the new hotness tho?

Adbot
ADBOT LOVES YOU

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

Plavski posted:

I thought Mongo was the new hotness tho?

I'm pretty sure that fell off pretty quickly when people realised schemas and data integrity are pretty hot.

It's not like Postgres is even lacking a JSON datatype.

an skeleton
Apr 23, 2012

scowls @ u
I like Mongo. I think its perfectly fine, but the app I built in it wasn't some crazy large-scale / financial / medical app so YMMV.

Another question: what is the consensus on tools such as Browserstack? Anyone have experience using it, was it worth it, or is there an alternative you liked better?

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
It may be better now but a year ago I pretty much wrote it off because testing on it didn't come close to:

- Internet Explorer VMs
- Vendor provided simulators
- The actual device

Primary reasons were it was laggy, the machine itself would be slow (making actually debugging an issue a nightmare), and after all that you still had hoops to jump through.

It's probably alright for just checking out one or two issues, but it's not nice for repeated use in my experience.

Kekekela
Oct 28, 2004
On a windows machine*, is there some simple way to remove all my local non-master branches from a git repository?






* everything I can find googling uses GREP

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Windows has findstr which might work if you can figure out the equivalent flags https://technet.microsoft.com/en-us/library/bb490907.aspx

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Maluco Marinero posted:

I'm pretty sure that fell off pretty quickly when people realised schemas and data integrity are pretty hot.

It's not like Postgres is even lacking a JSON datatype.

Not only that, but recent versions of Postgres JSONB data type is faster than Mongo, can be indexed, and supports updating a single field in a JSON object without requiring rewriting the entire object. AND on the off chance you need a real database, it's there too.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Skandranon posted:

Not only that, but recent versions of Postgres JSONB data type is faster than Mongo, can be indexed, and supports updating a single field in a JSON object without requiring rewriting the entire object. AND on the off chance you need a real database, it's there too.

Yeah, JSONB is pretty fuckin awesome and I don't even know why Mongo exists anymore.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Thermopyle posted:

Yeah, JSONB is pretty fuckin awesome and I don't even know why Mongo exists anymore.

Uh, because it's webscale :smuggo:

qntm
Jun 17, 2009
Is it okay to use a two-way data binding in an Angular component? The documentation is giving me mixed messages.

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

qntm posted:

Is it okay to use a two-way data binding in an Angular component? The documentation is giving me mixed messages.

The world won't end, but you should probably try to use the < and & instead of =, especially if you are trying to migrate to Angular 2.

Edit: If you want more reasons for why... = binding is rather confusing and not doing what you think. It does not pass by reference. It creates objects at either end, and then sets up $watch to look at both and run code to sync during digests. This will trigger when you do x=5 or x={ name:"zzz" }, but will NOT trigger when you do x.name="zzz". You can make the watches created watch the entire object using =*, but this is hugely expensive because then Angular iterates over all fields, and all members of those fields, to see if any of them changed, every digest. < passes by reference so a) you sorta get 2 way binding anyways and b) doesn't need $watches to keep them in sync. & allows for a much more controlled way of controlling how a child talks to it's parent, and mirrors how bindings would be accomplished in Angular 2.

Also yeah, see below link for even more explanations.

Skandranon fucked around with this message at 19:56 on Dec 13, 2016

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

Skandranon posted:

The world won't end, but you should probably try to use the < and & instead of =, especially if you are trying to migrate to Angular 2.
In line with this, this post by Todd Motto is super-informative on best practices for bindings in Angular 1.5+ components: https://toddmotto.com/angular-1-5-lifecycle-hooks

I used to be a big fan of two-way bindings but after reading this article and fixing "issues" caused by two-way data binding with a one-way binding and callback instead, I'm convinced that < and & are the way to go.

Count Thrashula
Jun 1, 2003

Death is nothing compared to vindication.
Buglord
Are there any issues with Node and Postgres? Or is there a better backend I should be using for Postgres?

The basic tutorial code from the pg module (github brianc/node-postgres) keeps failing authentication, and even fails if I explicitly provide a known-good username and password. I've seen Github comments about authentication being wonky but that was months ago and was supposedly fixed, and I'm using the latest node and pg.

luchadornado
Oct 7, 2004

A boombox is not a toy!

COOL CORN posted:

Are there any issues with Node and Postgres? Or is there a better backend I should be using for Postgres?

The basic tutorial code from the pg module (github brianc/node-postgres) keeps failing authentication, and even fails if I explicitly provide a known-good username and password. I've seen Github comments about authentication being wonky but that was months ago and was supposedly fixed, and I'm using the latest node and pg.

I don't see why Node + Postgres should have any issues, without having ever tried it before. As with anything open source, assume the tutorial is out of date and try to suss it out yourself by looking at the API documentation. You could also try a different version of node-postgres, check the Issues tab in Github, post to a Gitter account if one exists. I've never had any issues with Postgres + Python/.NET/Java, so that's why it'd surprise me if there were fundamental flaws with Node.

edit: looking at the changelog and issues, they may have some problems with connections in certain cases in the 6.X releases. 5.2 may be more of a "stable" release.

luchadornado fucked around with this message at 01:32 on Dec 14, 2016

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Plavski posted:

I thought Mongo was the new hotness tho?

The Mongo is Web Scale video is 6 years old.

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

COOL CORN posted:

Are there any issues with Node and Postgres? Or is there a better backend I should be using for Postgres?

The basic tutorial code from the pg module (github brianc/node-postgres) keeps failing authentication, and even fails if I explicitly provide a known-good username and password. I've seen Github comments about authentication being wonky but that was months ago and was supposedly fixed, and I'm using the latest node and pg.

I recently built a REST front-end for PostgreSQL 9.5 in both NodeJS and C# .Net Core, didn't have any issues with either. What node package did you use to connect to PostgreSQL? I used pg-promise and it was quite easy.

Edit: link for tutorial http://mherman.org/blog/2016/03/13/designing-a-restful-api-with-node-and-postgres/

code:
let promise = require("bluebird");

let options = {
    // Initialization Options
    promiseLib: promise
};
let pgp = require("pg-promise")(options);
let connectionString = "postgres://postgres:password@localhost:5432/database";
let db = pgp(connectionString);

Skandranon fucked around with this message at 02:41 on Dec 14, 2016

well why not
Feb 10, 2009




Kekekela posted:

On a windows machine*, is there some simple way to remove all my local non-master branches from a git repository?






* everything I can find googling uses GREP

What's wrong with git branch -d {branchname} ?

Pollyanna
Mar 5, 2005

Milk's on them.


We're thinking of writing React components that can be served from a CDN and easily included on webpages independent of the components themselves, like how Google Analytics works. I don't see much about this out there, are there reasons why we wouldn't want to do this? It would decouple our team from the work that other teams are doing and make life a lot easier for all of us.

The basic idea is that you'd add a script tag that references the CDN for the component you want, then add a div with a particular ID whereever you want the component to render. Basically a drop-in approach.

Anony Mouse
Jan 30, 2005

A name means nothing on the battlefield. After a week, no one has a name.
Lipstick Apathy
Sounds fine to me, in fact it leverages some of the main advantages of React - being able to apply it to as little or as much of your app as you want, completely independent of other state or components. The only downside might be a "pop-in" effect as components load asynchronously, but that can be mitigated with some UX design.

Kekekela
Oct 28, 2004

well why not posted:

What's wrong with git branch -d {branchname} ?

I had a shitton and didn't want to have to do that for each one.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Yeah, ain't nothing wrong with that, although if you want to make it pop less design the containers a little to maintain some sort of sizing or look.

We used React components in a partial way on this site: https://archierose.com.au , check out experiences, contact or tailored spirits to see what I mean. We render everything with react though, which makes it really easy for us to avoid 'pop-in', if you look at the page we shuttle the props in through serialised data attributes, and also make the contents mountable by make it accurate to what the client renders too.

We could probs do more on the efficiency/optimisation front but this is just to demonstrate that idea of using React for small components on page. It actually works real well aside from maybe having to do some extra work to allow the pieces to talk to each other if you need them to.

Pollyanna
Mar 5, 2005

Milk's on them.


After experimenting a bit, it's totally possible to expose individual React components via a remote CDN endpoint, but it takes some weird finagling and JavaScript wizardry to get it to work in any sophisticated manner (e.g. specifying the element to attach to, data attributes, etc.) and you're likely to end up with something that halfassedly replicates the functionality of a module loading system like AMD or Require.js anyway. At that point, you might as well go with a React app from the top down and publish the components as npm modules.

It looks like yes, technically you can do it, but I have no idea if it's worth it yet for our purposes. Of course, we're trying to do something that's kinda weird (totally independent development teams for the same webpage/collection thereof), so...YMMV.

Edit: If it's possible to load a remote .js file in JavaScript and store it locally, kind of like a bullshit version of modules, then it would work as long as the app has some JavaScript functionality itself.

Pollyanna fucked around with this message at 23:08 on Dec 15, 2016

geeves
Sep 16, 2004

an skeleton posted:

its a bit of an overwrought process, but basically they upload the image directly to s3 and let us now when that's done, so theres a few different things going on there. i think we've decided on doing an image proxy where we'll be rotating the pictures appropriately on the backend after upload, tl;dr probably won't need to resort to css magic.

There's an open source project called Openross that's a python microservice that sits in front of S3 as an image viewer that we've been using. It's pretty straightforward and if you're familiar with python it's easy to extend.

Typing in phone now, but check the forks that open up the image type to all types - the master is just jpeg.

I'm actually working on a fork to add in better cropping and rotation detection.

Edit: the main openross github has been removed (just in the last few weeks), but the forks live on: https://github.com/gulbinas/openross/network

geeves fucked around with this message at 01:31 on Dec 16, 2016

Roadie
Jun 30, 2013
I've been working on an app built around Vue.js and Typescript, and boy are single-file components and strict typing beautiful once you gently caress around enough to get it all auto-building with Webpack and stuff, and our designer guy tells me that easily scoped styling makes his job way easier. Plus, the clear separation between methods/computed values/props/data is (IMO) way clearer than React's quasi-HTML soup.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Roadie posted:

I've been working on an app built around Vue.js and Typescript, and boy are single-file components and strict typing beautiful once you gently caress around enough to get it all auto-building with Webpack and stuff, and our designer guy tells me that easily scoped styling makes his job way easier. Plus, the clear separation between methods/computed values/props/data is (IMO) way clearer than React's quasi-HTML soup.

But you get all that with React too? Maybe I've been using wrong :v:

luchadornado
Oct 7, 2004

A boombox is not a toy!

Yeah, you can (and should) do that with React too. To clarify, we've been using a variation of the "ducks" pattern along with "selectors" in the duck:

https://github.com/erikras/ducks-modular-redux

Our components are pure functions, stateless (except in rare cases), and have minimal/no logic.

Typescript 2.1 is out and has async/await, object rest/spread, and keyof! We're actually at the point now where we're starting to yank Babel out of projects when we can just use Typescript compilation, and it feels good.

luchadornado fucked around with this message at 21:45 on Dec 16, 2016

Roadie
Jun 30, 2013

Helicity posted:

Our components are pure functions, stateless (except in rare cases), and have minimal/no logic.
See, when I think "component", I think a self-contained thing that can have its own internal operations, state, whatever. For example: toggle button. Click a thing, an animation happens, the internally-held value changes to an appropriate thing, events get passed up if appropriate...

A lovely Vue.js example:
code:
/** ToggleButton.vue */

<template>
  <div class="ToggleButton">
    <input type="radio" name="ToggleButton" value="on" v-model="value"> On
    <input type="radio" name="ToggleButton" value="off" v-model="value"> Off
  </div>
</template>

<script>
  export default {
    props: {
      value {
        type: string,
        default: undefined
      }
    },
    watch: {
      value: function (val) {
        if (val) {
          // put an animation here, or maybe intercept the input event instead
          this.$emit('value', val)
        }
      }
    }
  }
</script>

<style scoped>
// the JS-illiterate design guy's CSS goes here to make it look fancy
</style>
code:
/** In some other file */

<ToggleButton v-model="thisWillBeUndefinedOrOnOrOff"></ToggleButton>

Helicity posted:

https://github.com/erikras/ducks-modular-redux
I'll be honest: to me this is just complete gibberish.

Helicity posted:

Typescript 2.1 is out and has async/await
Async/await is definitely a godsend for a lot of stuff.

Roadie fucked around with this message at 02:44 on Dec 17, 2016

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Roadie posted:

See, when I think "component", I think a self-contained thing that can have its own internal operations, state, whatever. For example: toggle button. Click a thing, an animation happens, the internally-held value changes to an appropriate thing, events get passed up if appropriate...

That's how React started out but everyone figured out that it doesn't scale well.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
After a little over a month of this, I've reached a point where I think I can understand deeper answers to the otherwise beginner questions I have been wanting to ask. I will be dropping some of these questions over the next few days as I'm able to type them up. My first bit is about authentication in AngularJS, which I did bring up a little bit earlier, but I now understand a little more.

I wound up doing basic authentication to get a session id from my server, which I then persisted in a cookie. The session could get invalidated, which would compel the user to log in again. I don't think my logic to make the prompt come back up works yet. I am assuming there's a little hook I can put into django on the admin side to invalidate all sessions so I can verify this. That's my problem.

The thing that got me was how hard it was to do all this. Well, a lot of that was me being knew, but the code to do it still contrasts itself sharply with the code I'm normally writing. It made me feel like it was the kind of thing people don't normally do. Given what I'm doing so far is not sophisticated, this makes me very worried that I'm doing something incorrectly unconventional. To do basic authentication, I had to get an encode function to encode the username:password string so I could send it in the http header. Having to set the header seemed strange to me. I would have figured there was a built-in for this since I imagine most sites have to do something like this. Are there other methods being used these days to do all this? The only alternative I can think is an explict REST call to log in that sends the login data as JSON in the HTML data section.

Anyways, I get the session id and store it in a cookie along with my other stuff. The design for this was frustrating. I started by creating a service and had it wait on events for logging in, logging out, session expiration, login failures, and then for the page to load. Depending on what happened there, I would be creating a cookie, deleting it, replacing it, extracting it, or whatever. No fuss. However, I was surprised none of the code ended up running. I figured the service won't actually get instantiated until it was referenced somewhere in a controller. The login controller seemed like a place for this, but I felt awkward cramming a reference into there--particularly to not even bother using it. I also did have some separation of concerns I liked to keep here. My service persisted the session, my controller managed the login. Since the login controller I used did not have all the logic to interact with the service, it seemed even more awkward to connect it there. Again, I had that service working entirely through events.

I ended up making it a controller, and just shoving a div tag referencing it in my login header HTML. That did the trick, but leaves me with a foul taste. Now I have this little dingleberry in the .html that has no direct interactions that just sits around and reacts to things.

The other issue I had that should be more clear-cut is differentiating $rootScope and $scope--in particular to event-driven communication. The events in the service controller were using $rootScope. However, I saw a lot of stuff poopoo'ing using the root scope. Using $scope wasn't going to help since it would just be talking to itself, right? The controller is just mentioned inside it's own div tag prison. Is there some other method that I should debut the event and connect it? I find poopoo using a global context for events to be odd since, well, I'm used to events being fired across a global context. Is there some kind of channel mechanism or something to restrict the scope of events?

I think I would be generally interested in seeing how people are sharing their data between controllers. The "no two controllers talking to each other directly" thing messed me up in all of this.

The Fool
Oct 16, 2003


Rocko Bonaparte posted:

After a little over a month of this, I've reached a point where I think I can understand deeper answers to the otherwise beginner questions I have been wanting to ask. I will be dropping some of these questions over the next few days as I'm able to type them up. My first bit is about authentication in AngularJS, which I did bring up a little bit earlier, but I now understand a little more.

I wound up doing basic authentication to get a session id from my server, which I then persisted in a cookie. The session could get invalidated, which would compel the user to log in again. I don't think my logic to make the prompt come back up works yet. I am assuming there's a little hook I can put into django on the admin side to invalidate all sessions so I can verify this. That's my problem.

The thing that got me was how hard it was to do all this. Well, a lot of that was me being knew, but the code to do it still contrasts itself sharply with the code I'm normally writing. It made me feel like it was the kind of thing people don't normally do. Given what I'm doing so far is not sophisticated, this makes me very worried that I'm doing something incorrectly unconventional. To do basic authentication, I had to get an encode function to encode the username:password string so I could send it in the http header. Having to set the header seemed strange to me. I would have figured there was a built-in for this since I imagine most sites have to do something like this. Are there other methods being used these days to do all this? The only alternative I can think is an explict REST call to log in that sends the login data as JSON in the HTML data section.

Anyways, I get the session id and store it in a cookie along with my other stuff. The design for this was frustrating. I started by creating a service and had it wait on events for logging in, logging out, session expiration, login failures, and then for the page to load. Depending on what happened there, I would be creating a cookie, deleting it, replacing it, extracting it, or whatever. No fuss. However, I was surprised none of the code ended up running. I figured the service won't actually get instantiated until it was referenced somewhere in a controller. The login controller seemed like a place for this, but I felt awkward cramming a reference into there--particularly to not even bother using it. I also did have some separation of concerns I liked to keep here. My service persisted the session, my controller managed the login. Since the login controller I used did not have all the logic to interact with the service, it seemed even more awkward to connect it there. Again, I had that service working entirely through events.

I ended up making it a controller, and just shoving a div tag referencing it in my login header HTML. That did the trick, but leaves me with a foul taste. Now I have this little dingleberry in the .html that has no direct interactions that just sits around and reacts to things.

The other issue I had that should be more clear-cut is differentiating $rootScope and $scope--in particular to event-driven communication. The events in the service controller were using $rootScope. However, I saw a lot of stuff poopoo'ing using the root scope. Using $scope wasn't going to help since it would just be talking to itself, right? The controller is just mentioned inside it's own div tag prison. Is there some other method that I should debut the event and connect it? I find poopoo using a global context for events to be odd since, well, I'm used to events being fired across a global context. Is there some kind of channel mechanism or something to restrict the scope of events?

I think I would be generally interested in seeing how people are sharing their data between controllers. The "no two controllers talking to each other directly" thing messed me up in all of this.

I've never used it myself, but Django has built in user authentication, and there are roughly a billion articles about using it with Angular.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

The Fool posted:

I've never used it myself, but Django has built in user authentication, and there are roughly a billion articles about using it with Angular.

Yes--I was going through them. I was just surprised that prompting for a username and password, and saving that information across payloads, was not some basic API thing in YOOL 2016. This is especially because I would consider stuff like that to be sensitive and likely to be screwed up in insecure ways. I would have figured it would be wrapped in some standard calls. My perspective is coming from never doing this kind of thing before now and having set up some expectations from these frameworks before reaching user authentication. That's pretty much all me, but I'm still going to run my mouth about it on the Internet for a little bit.

Generally speaking, the host side with Django was less of a surprise to me.

lunar detritus
May 6, 2009


Helicity posted:

Yeah, you can (and should) do that with React too. To clarify, we've been using a variation of the "ducks" pattern along with "selectors" in the duck:

https://github.com/erikras/ducks-modular-redux

Our components are pure functions, stateless (except in rare cases), and have minimal/no logic.

Typescript 2.1 is out and has async/await, object rest/spread, and keyof! We're actually at the point now where we're starting to yank Babel out of projects when we can just use Typescript compilation, and it feels good.

Oh, the duck pattern looks very interesting. The main problem my boss has with the redux way of doing things (we're using ngrx/store) is the amount of boilerplate and extra files.

I wish I could use Typescript 2.1 but Angular doesn't support it yet. I had to roll back a couple of things because of AoT breaking. :negative:

luchadornado
Oct 7, 2004

A boombox is not a toy!

Roadie posted:

See, when I think "component", I think a self-contained thing that can have its own internal operations, state, whatever.

You can do that in React too, but as Thermopyle mentioned, it gets really difficult to work with once you scale out a little beyond Hello World. Keeping as many components stateless as possible truly makes your job easier. Redux will help you do that with anything driven by application state, but it's not very useful to throw UI state (like your toggle example) into Redux. Keep that state local to your components if you can. There is unfortunately no silver bullet for dealing with things like forms in a one-way data flow - I've tried several libs, but none have been pleasant.

quote:

I'll be honest: to me this is just complete gibberish.

Long and short of it is, you have:

Actions: a description of an event and the any optional data as a result of the event that should do something with state
Reducers: Usually a switch block that looks for dispatched actions. Run your old state through reducers to reduce down to your new state.
Action creators: Things that dispatch actions
Selectors: Things that get a small piece of data out of the state

That duck pattern is just keeping all these extremely related things together in the same file. They handle most of your logic, and especially anything dealing with application (not UI) state. Then you have containers which can have more logic, but are typically just a way to map your state-derived props and your action creators to your component (React-Redux is super useful for this). So for a "User Details" page, you would have user-details-duck.js, user-details-container.jsx, and user-details.jsx or whatever floats your boat.

To gmq's point, Redux unfortunately promotes boilerplate, so any ways of reducing it are encouraged.

piratepilates
Mar 28, 2004

So I will learn to live with it. Because I can live with it. I can live with it.



Helicity posted:


Reducers: Usually a switch block that looks for dispatched actions. Run your old state through reducers to reduce down to your new state.

Oh poo poo. I've never used redux before but I've read a bunch of stuff about it and follow Dan Abramov on Twitter. I never really thought about what a reducer actually is, it's just a reduce function (like [].reduce(...)) with state as the collection. Whoops.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

piratepilates posted:

Oh poo poo. I've never used redux before but I've read a bunch of stuff about it and follow Dan Abramov on Twitter. I never really thought about what a reducer actually is, it's just a reduce function (like [].reduce(...)) with state as the collection. Whoops.

Exactly.

code:
const reducer = (existingState, action) => newState;
That's it. In Redux land, if you have any "connected components" they will be notified of the state change, re-run mapStateToProps and re-render if needed.

As for boilerplate.... snipmate exists.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

The redux boilerplate isn't that big of a deal if you just use your IDE and/or JS features to eliminate or reduce it.

Summit
Mar 6, 2004

David wanted you to have this.
Forgive my React ignorance, just started working in it in my spare time (stories are sparse at work end of year). Does MobX solve these boilerplate problems? Granted a lot of the details are hidden/magic but it's been working great for me thus far without any need for reducers, actions, etc.

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Rocko Bonaparte posted:

I think I would be generally interested in seeing how people are sharing their data between controllers. The "no two controllers talking to each other directly" thing messed me up in all of this.

Ok, I hate to have to put it this way, but you've done everything wrong wrong wrong. You should not be using $rootScope, you should be using a service to store your session information, you shouldn't be using a controller, and controllers should not directly talk to each other.

To start, a Service is a singleton that is instantiated when referenced, either by a Controller or another Service. Since in your case, you want your login to happen for the application as a whole, it makes sense to have your service running all the time. To accomplish this without strange klug divs & controllers around, what you want to do is have your LoginService referenced by the .run() function of your angular.module. This will bootstrap your LoginService before any DOM rendering takes place. You can then have this LoginService referenced by any other parts of your application that might care, and they can then make decisions on how to respond to your login state.

This solves your information sharing issue, your $rootScope issue, and your bootstrapping kludge.

ToxicSlurpee
Nov 5, 2003

-=SEND HELP=-


Pillbug
I'm kind of curious about something so I figure I'll ask...have any of you used Backbone and what are your opinions on it?

I've been using it lately and I actually quite like it.

Adbot
ADBOT LOVES YOU

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

Skandranon posted:

Ok, I hate to have to put it this way, but you've done everything wrong wrong wrong. You should not be using $rootScope, you should be using a service to store your session information, you shouldn't be using a controller, and controllers should not directly talk to each other.

To start, a Service is a singleton that is instantiated when referenced, either by a Controller or another Service. Since in your case, you want your login to happen for the application as a whole, it makes sense to have your service running all the time. To accomplish this without strange klug divs & controllers around, what you want to do is have your LoginService referenced by the .run() function of your angular.module. This will bootstrap your LoginService before any DOM rendering takes place. You can then have this LoginService referenced by any other parts of your application that might care, and they can then make decisions on how to respond to your login state.

This solves your information sharing issue, your $rootScope issue, and your bootstrapping kludge.

This isn't the end of the world to change. I basically just rename my controller to a service and bring it up in the .run() function. I didn't know about that function at all. Refactoring the service so it doesn't just do its own thing with existing events is more elaborate though, but not the end of the world. This is the kind of stuff I want to know about since I don't have anybody else in my organization to check about this kind of stuff. All I have is a basis is contradictory ways of doing things that I find online.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply