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
ModeSix
Mar 14, 2009

Skandranon posted:

I know I often sound like a boring, unfun old man, but this is bad. You should strive to keep such implementation details out of your HTML. One of Angular's big mistakes is the "ToDo app with NO CODE". Of course there is code, it's just implicit in the HTML, which leads to big problems for anything real.

If item 3 is special, you should probably extract it to a specific variable. If you are trying to show all the items, you should iterate over the collection.

In general, your templates should contain a minimum of logic, and just reference properties in your controller. Your controller is responsible for preparing such properties for the template. Services should be used to store any data that is not ONLY for display purposes.

So it would be better to pull the array item to a var in the controller and reference that var in the HTML is what you're saying?

I just need the one item displayed in this particular case as it's the featured item on the main page and has nothing to do with the menu page at all.

We are actually using services to store the data, and the teacher wants us to use controllers to access it, which is the proper way of doing it isn't it?

The problem I seem to be having is specifically how to use the controllers to access the data in the way I want to/need to. This is what this weeks assignment is, is access the data stored in a service by using a controller, and it's been giving me more grief than it rightly should.

HaB posted:

K. That's completely unclear as a third party reading your code.

If getDish returns an array, name it getDishes

It seems small and nitpicky but little stuff like that will drive you insane down the road. Cultivate those habits now and you won't have to worry about it.

See also everything Skandranon just said.

I see what you mean.

getDish returns one item from an array, getDishes returns the whole array for iteration. (I actually already have both of these)

I appreciate both of your input, will work on it. Small steps towards the bigger picture. Angular is moderately confusing for me in the way to do things, so I guess sometimes I take the easy route rather than the proper/good route. (I'll try to do this less)

I'll keep asking things here when I'm not exactly sure. :v:

ModeSix fucked around with this message at 06:08 on Feb 25, 2016

Adbot
ADBOT LOVES YOU

HaB
Jan 5, 2001

What are the odds?

ModeSix posted:

So it would be better to pull the array item to a var in the controller and reference that var in the HTML is what you're saying?

I just need the one item displayed in this particular case as it's the featured item on the main page and has nothing to do with the menu page at all.

We are actually using services to store the data, and the teacher wants us to use controllers to access it, which is the proper way of doing it isn't it?

The problem I seem to be having is specifically how to use the controllers to access the data in the way I want to/need to. This is what this weeks assignment is, is access the data stored in a service by using a controller, and it's been giving me more grief than it rightly should.


I see what you mean.

getDish returns one item from an array, getDishes returns the whole array for iteration. (I actually already have both of these)

I appreciate both of your input, will work on it. Small steps towards the bigger picture. Angular is moderately confusing for me in the way to do things, so I guess sometimes I take the easy route rather than the proper/good route. (I'll try to do this less)

I'll keep asking things here when I'm not exactly sure. :v:

I think where both of us are having trouble understanding what you're doing is - you just said getDish() returns a single item, which you are storing in featuredItem.

But your html snippet initially had you looping over item in featuredItem. What is there to loop over if getDish doesn't return an array and featuredItem is a single item?

Then you went with featuredItem[3].name. If featuredItem is a single item, why do you need the array index? And why do you need to ng-repeat at all? Why not:

code:

var featuredItem = menuFactory.getDish(parseInt(0, 10));

...

{{ featuredItem.name }}

In fact I'm pedantic enough to really want:

code:

var featuredItem = menuFactory.getFeaturedItem();

because that says EXACTLY what you are doing, and getDish(parseInt(0, 10)) makes me wonder where the parseInt args are coming from, plus magic numbers, etc. Also it keeps logic out of your controller. The menuFactory can hold the logic for determining what the featured Item is on any given day.

As far as accessing data from a service - you're basically doing it correctly. Inject the service into your controller and call the service's methods to get whatever you are looking for, then use the Controller to prepare your data for display in that Controller's view.

ModeSix
Mar 14, 2009

HaB posted:

I think where both of us are having trouble understanding what you're doing is - you just said getDish() returns a single item, which you are storing in featuredItem.

But your html snippet initially had you looping over item in featuredItem. What is there to loop over if getDish doesn't return an array and featuredItem is a single item?

Then you went with featuredItem[3].name. If featuredItem is a single item, why do you need the array index? And why do you need to ng-repeat at all? Why not:

because that says EXACTLY what you are doing, and getDish(parseInt(0, 10)) makes me wonder where the parseInt args are coming from, plus magic numbers, etc. Also it keeps logic out of your controller. The menuFactory can hold the logic for determining what the featured Item is on any given day.

As far as accessing data from a service - you're basically doing it correctly. Inject the service into your controller and call the service's methods to get whatever you are looking for, then use the Controller to prepare your data for display in that Controller's view.

Basically the way I did it was a big mess and I am going to completely remove what I've got and redo it to make some actual sense based on what everyone here told me.

I think I banged my head against it for too long and went the "overly complicated and doesn't make any loving sense but it displays what I want" route. I was really having problems getting my head around how to get the data I wanted to display. I even avoided this route at one point when one of my classmates suggested it to me.

I've trimmed it down to this:
code:
 .controller('IndexController', ['$scope', 'menuFactory', 'corporateFactory', function($scope, menuFactory, corporateFactory) {
            
            $scope.featuredItem = menuFactory.getDish(0);
           
        }])
And in my html:
code:
{{featuredItem.name}}
It's clear what this tag is for, no?

ModeSix fucked around with this message at 14:05 on Feb 25, 2016

HaB
Jan 5, 2001

What are the odds?

ModeSix posted:

Basically the way I did it was a big mess and I am going to completely remove what I've got and redo it to make some actual sense based on what everyone here told me.

I think I banged my head against it for too long and went the "overly complicated and doesn't make any loving sense but it displays what I want" route.

Heh. Welcome to Programming. Snack table is to the left. Happy Hour starts at 5:30 SHARP!

:v:

Edit: you added code after I quoted. Html is very clear now. Still not a fan of that magic 0, but it's far more readable now than what you started with.

ModeSix
Mar 14, 2009

HaB posted:

Heh. Welcome to Programming. Snack table is to the left. Happy Hour starts at 5:30 SHARP!

:v:

Edit: you added code after I quoted. Html is very clear now. Still not a fan of that magic 0, but it's far more readable now than what you started with.

Well I think that magic 0 is eventually going to turn into something that changes per day, but for now it's just a static reference to the item 0 in dishes array as that is the scope of the assignment for now.

The assignment is actually pulling data from 3 different places for display on the index.html, snippets from various services/data that are used in other pages.

Edit: The scope of the course is actually far broader than angular. We'll eventually be moving into backend NodeJS/MongoDB implementation with the project we're building right now.

ModeSix fucked around with this message at 14:29 on Feb 25, 2016

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

ModeSix posted:

Well I think that magic 0 is eventually going to turn into something that changes per day, but for now it's just a static reference to the item 0 in dishes array as that is the scope of the assignment for now.

The assignment is actually pulling data from 3 different places for display on the index.html, snippets from various services/data that are used in other pages.

A lot of programming is naming things well. Spend some time and think about how things should be named so that someone else can understand what you are doing. This will help others and yourself in the long run.

ModeSix
Mar 14, 2009

Skandranon posted:

A lot of programming is naming things well. Spend some time and think about how things should be named so that someone else can understand what you are doing. This will help others and yourself in the long run.

Yeah, I definitely learned this when I was doing app development and set a project aside for a month and came back to it and couldn't figure out my own code, let alone someone elses.

It's pretty bad when you look at your own code and say "wtf was I trying to do here?"

Prel
Jan 12, 2012

ModeSix posted:

So it would be better to pull the array item to a var in the controller and reference that var in the HTML is what you're saying?

I just need the one item displayed in this particular case as it's the featured item on the main page and has nothing to do with the menu page at all.

We are actually using services to store the data, and the teacher wants us to use controllers to access it, which is the proper way of doing it isn't it?

The problem I seem to be having is specifically how to use the controllers to access the data in the way I want to/need to. This is what this weeks assignment is, is access the data stored in a service by using a controller, and it's been giving me more grief than it rightly should.

This is a little old now but worth a read: http://angular-tips.com/blog/2013/08/consuming-services/

It helped me wrap my head around some of the complications with accessing data stored in services from controllers and addresses a couple of ways you can deal with it. I'm still very much learning Angular so these methods may not be ideal or may have been replaced by better methods at this point, but it's still a good "halp wtf is going on" primer.

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

Prel posted:

This is a little old now but worth a read: http://angular-tips.com/blog/2013/08/consuming-services/

It helped me wrap my head around some of the complications with accessing data stored in services from controllers and addresses a couple of ways you can deal with it. I'm still very much learning Angular so these methods may not be ideal or may have been replaced by better methods at this point, but it's still a good "halp wtf is going on" primer.

Services are actually really simple. TypeScript classes makes it a lot more obvious, but the reasoning applies either way. A service is just an object, a singleton, that is only created once. It is effectively a global variable, just better defined and has structure. If a controller has a reference to the service, it can read/write stuff in it. Services never go away (until the browser window is closed), so this is where you store data that matters.

bomblol
Jul 17, 2009

my first crapatar

Skandranon posted:

A lot of programming is naming things well. Spend some time and think about how things should be named so that someone else can understand what you are doing. This will help others and yourself in the long run.

I'm a TA for a course at my university thats meant to teach kids proper coding style and best practices. Every assignment, about 8% of the grade is solely based on good naming.

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

bomblol posted:

I'm a TA for a course at my university thats meant to teach kids proper coding style and best practices. Every assignment, about 8% of the grade is solely based on good naming.

Should be 80%

Infected Mushroom
Nov 4, 2009

Skandranon posted:

Should be 80%

That's for using spaces over tabs

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
Has anyone experimented with draft-js? It's a bit tricky to get HTML out of it, so someone wrote backdraft-js, but since it doesn't work both ways you can't actually initialize the editor with previously stored rich text. Am I missing something?

Anony Mouse
Jan 30, 2005

A name means nothing on the battlefield. After a week, no one has a name.
Lipstick Apathy
Hope this is the right place for this, looking for a critique of this React design pattern.

https://output.jsbin.com/lobuce

The goal is to have an infinitely scrolling component whose inner elements are derived solely from the scroll position. Imagine instead of being "Element Number X" they are thumbnail images, like <img src="X.jpg" />. All of the scrollable elements are exactly the same pre-known size which makes things a bit easier.

My main worry is the performance cost of deriving the elements in render(), but I can't think of a more elegant way to keep track of it in state.

code:
var Element = React.createClass({
  render() {
    return (
      <div style={{width: "98px", height: "98px", border: "1px solid black"}}>Element number {this.props.label}</div>
    );
  }
});


// An infinite scroll component
// The inner elements to display are derived solely from the current scroll position
var App = React.createClass({
  getInitialState() {
    return {scrollPos: 0}
  },
  render() {
    console.log("render");
    var data = [];
    // displayed elements are derived from scroll position and offset by 'i'
    for(var i = 9; i >= 0; i--){
      data.push(Math.round(((this.state.scrollPos - 50) / 100) + i));
    }
    
    return (
      <div style={{textAlign: "center"}}>
      Scroll me!
        <div style={{width: "100px", height: "800px", position: "relative", background: "#aaaaaa", overflow: "hidden", margin: "20px auto"}} onWheel={this.handleWheel}>
          <div style={{transform: "translateY("+(this.state.scrollPos % 100 - 100)+"px)"}}>
            {data.map(function(ea){
              return <Element key={ea} label={ea} />
            })}
          </div>
        </div>
      </div>
    );
  },
  handleWheel(e) {
    e.preventDefault();
    // update scroll position and keep it from going below zero
    this.setState({scrollPos: Math.max(this.state.scrollPos + e.deltaY, 0)});
  }
});


ReactDOM.render(<App />, document.getElementById("app-container"));

Anony Mouse fucked around with this message at 00:47 on Mar 1, 2016

Depressing Box
Jun 27, 2010

Half-price sideshow.
That reminds of the (much more complex) react-virtualized. It also does a lot of calculations in render(), albeit with some strategic memoization and componentShould/WillUpdate() checks to keep it under control.

Anony Mouse
Jan 30, 2005

A name means nothing on the battlefield. After a week, no one has a name.
Lipstick Apathy
So my method seems to work pretty well in my actual project, except I had to add shouldComponentUpdate returning false to prevent all of the inner elements from re-rendering unnecessarily.

Hyvok
Mar 30, 2010
I've been setting up OctoPrint for my 3D printing. OctoPrint is a web UI for your 3D printer done in mainly JS I guess. The bizarre thing is that it looks like it is not possible to limit anonymous users from seeing pretty much all information about the printing system including uploaded files, webcam view (!) etc. I'm not terribly familiar with modern web development but the explanation for this is "Well, contrary to what you'd first think it would not be enough to just refuse to display anything of the usual information in OctoPrint's interface to anonymous users. First of all, that info would still be accessible on the websocket that is used for pushing that information to the frontend.". What the hell does that mean? I assume it means that the "frontend" is the JS running in the browser and it queries the websocket for that information... But there has to be some simple way to perform authentication on that right? Instead of making all the information available to the whole drat internet? Just sounds a bit bizarre "reasoning" for everyone seeing your information by default.

piratepilates
Mar 28, 2004

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



Just force each websocket request from the client to include some kind of auth token and refuse the ones that aren't authorised/logged in/bad permissions. Same way you'd do it with a static (non-SPA) front-end.

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

Hyvok posted:

I've been setting up OctoPrint for my 3D printing. OctoPrint is a web UI for your 3D printer done in mainly JS I guess. The bizarre thing is that it looks like it is not possible to limit anonymous users from seeing pretty much all information about the printing system including uploaded files, webcam view (!) etc. I'm not terribly familiar with modern web development but the explanation for this is "Well, contrary to what you'd first think it would not be enough to just refuse to display anything of the usual information in OctoPrint's interface to anonymous users. First of all, that info would still be accessible on the websocket that is used for pushing that information to the frontend.". What the hell does that mean? I assume it means that the "frontend" is the JS running in the browser and it queries the websocket for that information... But there has to be some simple way to perform authentication on that right? Instead of making all the information available to the whole drat internet? Just sounds a bit bizarre "reasoning" for everyone seeing your information by default.

Websockets is a bi-directional active connection for both the UI and Server to push messages to each other. They might simply be referring to the fact that one can always open up dev-tools and view each websocket frame that is sent/received. It could also mean they have not implemented any sort of authentication in their server side websocket connection code, so ANYONE can open up a connection to the server and start sending and receiving data.

Riven
Apr 22, 2002

Infected Mushroom posted:

That's for using spaces over tabs

That's why you set tab to equal two spaces in settings.

Munkeymon
Aug 14, 2003

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



Apologies if this has been addressed already and I didn't understand and internalize the answer but I'm trying to get into the modern-style npm clusterfuck groove and just getting annoyed. I've cloned a project I want to contribute to and npm installed so I have all the modules I need to work with this thing, but I can't figure out how to get vscode to run them. I'm on Windows and don't want to install them all globally because I saw a bunch of deprecation warnings go by in the console spam. Is there a graceful way to run things from node_modules at all? google results all seem to be about making plugins for vscode itself.

Should I just not worry about making GBS threads up my environment with these old packages because npm is good at managing versions? (lol?)

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

Munkeymon posted:

Apologies if this has been addressed already and I didn't understand and internalize the answer but I'm trying to get into the modern-style npm clusterfuck groove and just getting annoyed. I've cloned a project I want to contribute to and npm installed so I have all the modules I need to work with this thing, but I can't figure out how to get vscode to run them. I'm on Windows and don't want to install them all globally because I saw a bunch of deprecation warnings go by in the console spam. Is there a graceful way to run things from node_modules at all? google results all seem to be about making plugins for vscode itself.

Should I just not worry about making GBS threads up my environment with these old packages because npm is good at managing versions? (lol?)

I have not done a whole lot with node.... but here is what my playing around in it have yielded.

You should have an index.js (or main.js, or something) that you tell node to run (could be done by typing "node main.js"). Any require statements within that file will pull in modules from the node_modules folder.

VSCode doesn't run anything, it's just a text editor. You can create some config files in the .vscode folder which will tell VSCode to run shell commands for you (like "node main.js"), but that's it.

Does the project not include any sort of documentation? There is probably something in the package.json that starts the thing.

Spraynard Kruger
May 8, 2007

Munkeymon posted:

Apologies if this has been addressed already and I didn't understand and internalize the answer but I'm trying to get into the modern-style npm clusterfuck groove and just getting annoyed. I've cloned a project I want to contribute to and npm installed so I have all the modules I need to work with this thing, but I can't figure out how to get vscode to run them. I'm on Windows and don't want to install them all globally because I saw a bunch of deprecation warnings go by in the console spam. Is there a graceful way to run things from node_modules at all? google results all seem to be about making plugins for vscode itself.

Should I just not worry about making GBS threads up my environment with these old packages because npm is good at managing versions? (lol?)

I'm a Windows/node guy and can probably help you out, but linking to the project would be super helpful (assuming it's public). If there's really something you need to run from node_modules, it'd be under node_modules/.bin/<thing_to_run>.cmd, but you're probably doing it wrong.

Munkeymon
Aug 14, 2003

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



I'm trying to work on https://github.com/berrberr/streamkeys which has a bunch of grunt tasks to build and package it, but I can't just "ctrl+p task dev" in vscode because I don't have grunt installed globally - just in the node_modules direcotry in my local copy.

Oh cool node-modules\.bin\grunt doesn't even exist :\ Guess I'll just start this over and see if it turns out different.

E: holy gently caress it contained 40k files?!

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

Munkeymon posted:

I'm trying to work on https://github.com/berrberr/streamkeys which has a bunch of grunt tasks to build and package it, but I can't just "ctrl+p task dev" in vscode because I don't have grunt installed globally - just in the node_modules direcotry in my local copy.

Oh cool node-modules\.bin\grunt doesn't even exist :\ Guess I'll just start this over and see if it turns out different.

Installing grunt globally shouldn't be a big deal. If you REALLY don't want to do that, you should be able to point your tasks.json to node_modules/bin/grunt

Spraynard Kruger
May 8, 2007

Munkeymon posted:

I'm trying to work on https://github.com/berrberr/streamkeys which has a bunch of grunt tasks to build and package it, but I can't just "ctrl+p task dev" in vscode because I don't have grunt installed globally - just in the node_modules direcotry in my local copy.

Oh cool node-modules\.bin\grunt doesn't even exist :\ Guess I'll just start this over and see if it turns out different.

E: holy gently caress it contained 40k files?!

Okay yeah, this looks like it wants you to have grunt-cli installed. Follow the directions at http://gruntjs.com/getting-started to get up and running.

Edit: As far as VS Code, I think I'd just treat it as a text editor for this project, then keep a separate cmd window open running the "grunt watch" task.

Munkeymon
Aug 14, 2003

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



I would just use Sublime because there's probably a package for local npm packages but I'm a dumb git baby and like the integration in vscode

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

Munkeymon posted:

I would just use Sublime because there's probably a package for local npm packages but I'm a dumb git baby and like the integration in vscode

You can hook up VSCode to node to do line by line debugging & breakpoints, if you need it.

Munkeymon
Aug 14, 2003

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



Figured it out! Needed grunt and grunt-cli installed globally and it just works with the node_modules stuff automatically (even through vscode) as long as node_modules exists, which it doesn't until you run npm install in the repo which is what I did first because that was in the readme. So the bit I was missing was the pre-existing global grunt(-cli) install which the readme was just assuming I'd have already.

piratepilates
Mar 28, 2004

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



I think you can just put the call to grunt in a run script inside the package.json and then do "npm run fart" and it will run it out of node-modules so you just need it installed locally instead of globally.

Alternatively installing it globally is fine for things like this where it's an actual application to run instead of a library (so it's fine to do it for grunt, but don't install task.js globally or something stupid like that). Not great, but fine for the most part.

Feral Integral
Jun 6, 2006

YOSPOS

Hey I'm designing a little chatroom app to get better at front end stuff. I have a rest service I'm hitting to get all the data for the page, is the best way to do this still to just bind some Javascript functions to the relevant elements and use jquery to do ajax calls to hit the rest service? Or what are people doing nowdays?

HaB
Jan 5, 2001

What are the odds?

Feral Integral posted:

Hey I'm designing a little chatroom app to get better at front end stuff. I have a rest service I'm hitting to get all the data for the page, is the best way to do this still to just bind some Javascript functions to the relevant elements and use jquery to do ajax calls to hit the rest service? Or what are people doing nowdays?

That's considered something of an old school approach these days. Now everyone uses one of the Javascript MVC libs - AngularJS (google's) or ReactJS (Facebook's). For styling - Bootstrap or Foundation are reliable things to have on a resume.

For a simple app all that might be overkill, but honestly - if you're going to be looking for a job doing front-end anytime soon - you will pretty much need to be familiar with at least ONE of those libraries. They are that ubiquitous.

Feral Integral
Jun 6, 2006

YOSPOS

HaB posted:

That's considered something of an old school approach these days. Now everyone uses one of the Javascript MVC libs - AngularJS (google's) or ReactJS (Facebook's). For styling - Bootstrap or Foundation are reliable things to have on a resume.

For a simple app all that might be overkill, but honestly - if you're going to be looking for a job doing front-end anytime soon - you will pretty much need to be familiar with at least ONE of those libraries. They are that ubiquitous.

So I would use a method like this: https://docs.angularjs.org/api/ng/service/$http ? Which would give me a promise with the data that I send to my javascript function which manipulates the page? Does that seem right, or am I missing something big about angular?

ModeSix
Mar 14, 2009

Feral Integral posted:

So I would use a method like this: https://docs.angularjs.org/api/ng/service/$http ? Which would give me a promise with the data that I send to my javascript function which manipulates the page? Does that seem right, or am I missing something big about angular?

That sounds about right, but you may want to look at ngResource for http calls and the like.

https://docs.angularjs.org/api/ngResource

Thermopyle
Jul 1, 2003

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

Providing a different way if doing http calls isn't really the point of these frameworks...

I'm on phone so I'm not going to do effort post, but modern frameworks should control the state and html.

To be honest, if you're looking to pick something up quick, React would probably be a better choice as its got a smaller API with easier to understand docs and best practices. That's not to say React is better in general, just on those two dimensions.

Feral Integral
Jun 6, 2006

YOSPOS

Thermopyle posted:

Providing a different way if doing http calls isn't really the point of these frameworks...

I'm on phone so I'm not going to do effort post, but modern frameworks should control the state and html.

To be honest, if you're looking to pick something up quick, React would probably be a better choice as its got a smaller API with easier to understand docs and best practices. That's not to say React is better in general, just on those two dimensions.

Aight I guess I don't really understand what people are really using angular for. In the past I've worked on django sites just rendering jinja templates to set the state of the page, but since this is supposed to be more of app, I'll need elements on the page to be dynamic without having to refresh the page. When this was necessary before, I'd just throw some javascript in the page to do ajax calls and update the page where it needed to be updated. I'm having trouble understanding how to replace this with something like angular, what advantages do I get?

PS: The more angular I get to use, the better - like you mentioned, it's used all over the place

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Feral Integral posted:

Aight I guess I don't really understand what people are really using angular for. In the past I've worked on django sites just rendering jinja templates to set the state of the page, but since this is supposed to be more of app, I'll need elements on the page to be dynamic without having to refresh the page. When this was necessary before, I'd just throw some javascript in the page to do ajax calls and update the page where it needed to be updated. I'm having trouble understanding how to replace this with something like angular, what advantages do I get?

PS: The more angular I get to use, the better - like you mentioned, it's used all over the place

Seconding starting with React over Angular if you have no explicit need to learn angular. React, as Thermo said is smaller, in that it's only the view, so you'll get more done faster, and when you are done, build it again in angular to see what's different and which you prefer. React coupled with Redux for state management is really easy to get your head around.

Lumpy fucked around with this message at 21:01 on Mar 6, 2016

piratepilates
Mar 28, 2004

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



Feral Integral posted:

Aight I guess I don't really understand what people are really using angular for. In the past I've worked on django sites just rendering jinja templates to set the state of the page, but since this is supposed to be more of app, I'll need elements on the page to be dynamic without having to refresh the page. When this was necessary before, I'd just throw some javascript in the page to do ajax calls and update the page where it needed to be updated. I'm having trouble understanding how to replace this with something like angular, what advantages do I get?

PS: The more angular I get to use, the better - like you mentioned, it's used all over the place

Back-end HTML rendering (i.e. jinja) with basic js event handlers works very well when you have something simple and static-y and there's nothing really bad about doing that. When you get in to something more interactive and stateful like a chatroom you can still very much do it with starting from back-end rendered HTML and adding the chat room features in plain js (no Angular, React, Ember, Knockout, Aurelia, Mithril, Meteor, Whatever) but you're probably going to run in to problems where your code gets messy and hard to reason about over time. The frameworks are just doing the work of figuring out better ways of structuring front-end code to run in to these problems less and to make it easier to add functionality that works well.

I'd probably recommend starting with React really, it's just a view-layer and very easy to get in to. You basically just write an object/class that describes how to take in data and how to return HTML, and then attach some functionality that will change the data over time (i.e. you add a method for handling a button click, in the method you send an ajax call [either use the new 'fetch' API, use the jQuery ajax since it's pretty good, or go through the trouble of creating XmlHttpRequest calls which is kind of a pain in the rear end], when the ajax call comes back you set the data on your object/class and React just renders the template again). Instead of having to create elements yourself and modify them over time and keep track of how they're modified over time (which is what would happen if you did it without a framework), React simplifies the process to just have to create a function to render a template from data, and then another for modifying the data.

If you want to go the Angular route it's not so bad (it's what I started with) but you may want to just start with only dealing with templates/controllers and maybe a service to handle the ajax calls yourself (may want to skip a provider until later since that's a whole nother set of angular stuff to learn which provides only a bit of advantage over doing the ajax calls yourself the way you already know how to do). Angular does sort of the same thing as React but less functional and focuses more on describing how components are rendered (the template/controller) from their properties that are being bound to things from other dependencies which are injected like services and providers.

I hope this helps answer your question of what the advantage over doing it all yourself is but I might have rambled on. Basically the frameworks just help structure your front-end code in a way that will get less messy than doing it all yourself.

Thermopyle
Jul 1, 2003

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

Feral Integral posted:

Aight I guess I don't really understand what people are really using angular for. In the past I've worked on django sites just rendering jinja templates to set the state of the page, but since this is supposed to be more of app, I'll need elements on the page to be dynamic without having to refresh the page. When this was necessary before, I'd just throw some javascript in the page to do ajax calls and update the page where it needed to be updated. I'm having trouble understanding how to replace this with something like angular, what advantages do I get?

PS: The more angular I get to use, the better - like you mentioned, it's used all over the place

You'll be able to understand this all more if you go through the React tutorial and/or maybe the Egghead React videos. It will be quicker to get up to speed with React than with Angular, then if you still want to use Angular, you'll get the whole point of these frameworks so you'll understand the more complex Angular better.

Really, everyone should be at least somewhat familiar with both frameworks nowadays.

I googled "point of Angular" and "point of React" and came up with the following. I just skimmed the links below so I can't guarantee they're good, but they seemed good after a quick look.

http://goo.gl/WUVV43
http://goo.gl/DHFFz7
http://goo.gl/PX4FVp

Thermopyle fucked around with this message at 18:25 on Mar 6, 2016

Adbot
ADBOT LOVES YOU

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice
Time to play "What stupid and obvious thing is Lumpy missing" react-router version!!

JavaScript code:
//client.js
import routes from './routes';
const target = document.getElementById('root');
ReactDOM.render(
    <Provider store={ store }>
            <Router history={hashHistory} routes={routes} />
    </Provider>,
    target
);


//routes.js
import { App } from '../containers/App';
import LoginForm from '../components/LoginForm';

export default(
    <Route path='/' component={App}>
        <IndexRoute component={LoginForm}/>
    </Route>
);

//App.js
const App = React.createClass({
    render: function () {
        return (
            <div className="mainContentArea">
                <h1>Why isn't this rendered?</h1>
                <p>lolz</p>
                {this.props.children}
            </div> 
        );
    }
});
export default App;


//LoginForm.js
import React from 'react';
export default () => (
        <p>WTF</p>
);
So, I should see a "Why isn't this rendered" header, a "lolz" paragraph, then a "WTF" paragraph, right? I've used react-router many, many times, and this is what I expect. Instead, all I see is 'WTF'.

What stupid, stupid thing have I done?

EDIT: As always, when I read my post, I instantly saw the problem!

JavaScript code:
// inside routes.js .....

// original
import { App } from '../containers/App'; /// <-- Lumpy is dumb!

// changed to
import App from '../containers/App'; /// <-- All better
Thanks for listening to me debug.

Lumpy fucked around with this message at 17:16 on Mar 11, 2016

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