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
The Fool
Oct 16, 2003


Dumb Lowtax posted:

OH forgot it was bitwise and not boolean for a second, whoops

Cool, but I guess a horror due to readability

Is everything like that considered too unreadable? Is it a horror to do

() => some_bool ^= 1

every time I want a callback to toggle a boolean value? I have lots of event handlers that do just that and it's nice for them to be so compact and seems logical enough to me



What's wrong with


code:

bool = !bool

Adbot
ADBOT LOVES YOU

Ola
Jul 19, 2004

If you need -2 for whatever reason, feel free to use ~true.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop

The Fool posted:

What's wrong with


code:
bool = !bool

With_a_long_enough_variable_name_that_becomes = !With_a_long_enough_variable_name_that_becomes

The Fool
Oct 16, 2003


I don't see the problem

necrotic
Aug 2, 2005
I owe my brother big time for this!

The Fool posted:

I don't see the problem

This. Don't be clever, be clear.

Ola
Jul 19, 2004

Dumb Lowtax posted:

With_a_long_enough_variable_name_that_becomes = !With_a_long_enough_variable_name_that_becomes

If only there was some way to quickly type out a long string.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Dumb Lowtax posted:

Is everything like that considered too unreadable? Is it a horror to do

() => some_bool ^= 1

every time I want a callback to toggle a boolean value? I have lots of event handlers that do just that and it's nice for them to be so compact and seems logical enough to me
Yes it is a horror.
code:
original_value = true;
some_bool = original_value;
some_bool ^= 1;
some_bool ^= 1;
alert(some_bool === original_value);  // This is false but with actual toggling would be true.
In typescript or javascript passed through the closure compiler with type validation doing this would be a handy compile-time error.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Ola posted:

If you need -2 for whatever reason, feel free to use ~true.
And if you need 4, why not simply use true<<-~true

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop

roomforthetuna posted:

// This is false but with actual toggling would be true.

Yikes, I guess the only thing that's saved me from that is that I haven't been using === at all

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Dumb Lowtax posted:

Yikes, I guess the only thing that's saved me from that is that I haven't been using === at all
There are also simpler side effects not involving ===, like if you intended to output "someone said your hair is green and that is false" you might instead output "someone said your hair is green and that is 0".

Munkeymon
Aug 14, 2003

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



Ola posted:

If you need -2 for whatever reason, feel free to use ~true.

And if you need true you can write !!-2

Sab669
Sep 24, 2009

Cross posting from the web dev thread :(

Sab669 posted:

having some weird issue where a specific page in my software is loading scrolled half way down the page.

There are a bunch of different divs all sandwhiched on top of each other (dashboard / record navigation, then the actual scrollable div)

cshtml file is like so:
HTML code:
<div id="dvIntFup>
  <div class="wrap-box">
    <div class="a bunch of classes">
      <table>
        i know i know it's 2019 why the gently caress are you using tables; i didn't design it and I don't have the resources to unfuck it
      </table>
      <div id="divPageScroll" class="scrollarea">
        <table>
          a bunch of more table bullshit
        </table>
      </div>
    </div>
  </div>
</div>
<div id="divPageScroll" class="scrollarea"> is the div with the scrollbar (desired), but for some reason it starts out half way down the page (undesired).

In the document.ready we call SetContainerDimensions('INTERIMFOLLOWUP', '', 'divPageScroll');

JavaScript code:
function SetContainerDimensions(fromWhere, grid, dvContainer, dvBottomContainer)
{
	var windowHeight = $(window).height() - 1;
	if (dvContainer != null && dvContainer != undefined)
	{
		if ($('.dashboard').length > 0)
		{
			var innerWindowHeight = windowHeight;
			innerWindowHeight = innerWindowHeight - 50; // Header height;
			var bodyHeight = innerWindowHeight - $('.dashboard').height();
			bodyHeight = bodyHeight - 30;

			// for interim followup screen there is a html content above to scroll area.
			// Reducing the height.
			if (fromWhere == 'INTERIMFOLLOWUP')
			{
				//Mantis #16217 change 
				bodyHeight = bodyHeight - 112;
				fromWhere = '';
			}

			$('.scrollarea').height(bodyHeight);
		}

		containerProperty = dvContainer
	}
}
CSS code:
.scrollarea {
    overflow-x: hidden;
    overflow-y: scroll;
    height: 300px;
}
I've tried window.scrollTo(0,0); and document.getElementById('divPageScroll').scrollTop = 0; but neither change the behavior at all? :shrug:

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Munkeymon posted:

And if you need true you can write !!-2
!~true is fun in that if you read it out loud it's "not not true" but its value is not not not true.

Kobayashi
Aug 13, 2004

by Nyc_Tattoo
I need help doing something stupid for a silly, backend/Node side project. I want one main, parent Typescript project that uses one or more standalone sub-projects, also written in Typescript. I'm basically trying to roll my own plugin system, just for fun.

As you might expect, I'm running into module errors. I'm not experienced enough to tell if this is a Node problem, a TSC/compiler problem, a VS Code task configuration problem, something I need to use a bundler for, something I need to code around, some combination of the above, or an impossible task. I think the problem involves separate, independent node_modules/ directories that are not getting picked up by TSC when it compiles .ts to .js files, but there are too many intersecting technologies for me to get much farther.

Any ideas or useful search terms?

code:
main_project/
  .vscode/
  node_modules/
  ...
  sub_project/
    node_modules/
    src/
      index.ts
    package.json
  ...
  src/
    index.ts
  package.json

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
you can start by posting the errors you're hitting, maybe

necrotic
Aug 2, 2005
I owe my brother big time for this!
Look at either yarn workspaces or the lerna tool. They are both tools to manage many related projects in one repo, and handles shared dependencies for you.

evilfunkyogi
Jun 27, 2005
:effort:
Are you using TypeScript's relatively new Project Reference API? I haven't used it, but it sounds like what you're trying to achieve.

Osmosisch
Sep 9, 2007

I shall make everyone look like me! Then when they trick each other, they will say "oh that Coyote, he is the smartest one, he can even trick the great Coyote."



Grimey Drawer
Alternatively, just use separate repos for your submodules and then use yarn/npm link to do symlinked development without having to push all the time?

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
I have a question about TypeScript. One of the benefits of a type system is making promises to the compiler for better performance, right? That sounds nice. With vanilla JS I run into performance issues with basic types that I make for math. Even declaring simple tuples of a few numbers (as a custom math vector type) in Vanilla JS is hard to do in a way that avoids an allocation, where it wouldn't end up on the heap and need garbage collection. Is TypeScript able to declare a small float tuple without allocating?

Munkeymon
Aug 14, 2003

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



Dumb Lowtax posted:

I have a question about TypeScript. One of the benefits of a type system is making promises to the compiler for better performance, right? That sounds nice. With vanilla JS I run into performance issues with basic types that I make for math. Even declaring simple tuples of a few numbers (as a custom math vector type) in Vanilla JS is hard to do in a way that avoids an allocation, where it wouldn't end up on the heap and need garbage collection. Is TypeScript able to declare a small float tuple without allocating?

TS just transpiles to JS, assuming all type constraints are satisfied, so it can do exactly what JS can do. I guess someday they might add the option to compile to Web Assembly or whatever they call it now, but right now it's a strict superset of JS.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Dumb Lowtax posted:

I have a question about TypeScript. One of the benefits of a type system is making promises to the compiler for better performance, right? That sounds nice. With vanilla JS I run into performance issues with basic types that I make for math. Even declaring simple tuples of a few numbers (as a custom math vector type) in Vanilla JS is hard to do in a way that avoids an allocation, where it wouldn't end up on the heap and need garbage collection. Is TypeScript able to declare a small float tuple without allocating?

The only way to avoid garbage collection is to not make garbage. If you're having issues with GC pauses, the first step is to remove any unnecessary "new" calls you're making. If you've already done that, you're going to need to implement object pooling, and reuse the objects instead of calling new every time - e.g. instead of doing new Point() or whatever, make everything use PointPool.Alloc() and PointPool.Recycle(), and just hydrate the recycled points in the alloc. If calling new() on your type is expensive or creating GC pressure, this will avoid that.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Setting up object pooling seems painful and bloated compared to how in C++ I can just say "Vector3 v;" in a function's scope and it will just harmlessly increase the size of that call stack frame and harmlessly disappear when the function ends. I assume it happens that way in JavaScript too when I declare something of type Number, but not much else because any other temp data object I make seems to depend on calling new(), even something as simple as "three floats". Is that one of the big compromises the language designers had to do to make JavaScript work? Designing it in a way that doesn't get to use the stack much?

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
BTW currently my solution is to have a couple static functions for generating a vector of 3 floats: one safe, and one unsafe. The "unsafe" one has to be consumed immediately because it always aliases to the same vector object, but it avoids an allocation. My object "pool" size is therefore 1, and it helps a tiny bit while saving my small Vector class implementation from having to confuse students (who will mostly never find out about the unsafe one) with a big object pooling algorithm inside of it, but it comes at a cost that most of my other Vectors are slow.

I also recently realized that my "in-fix" math operations like a.plus(b) require an allocation because they return a new Vector, so for inner loops I now provide alternative "pre-fix" math operations like a.add(b) that modify a vector in place. A little less readable for long math expressions, but it avoids the GC.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Dumb Lowtax posted:

Setting up object pooling seems painful and bloated compared to how in C++ I can just say "Vector3 v;" in a function's scope and it will just harmlessly increase the size of that call stack frame and harmlessly disappear when the function ends. I assume it happens that way in JavaScript too when I declare something of type Number, but not much else because any other temp data object I make seems to depend on calling new(), even something as simple as "three floats". Is that one of the big compromises the language designers had to do to make JavaScript work? Designing it in a way that doesn't get to use the stack much?

Yeah, it sucks. It's not a unique constraint to javascript though - any garbage collected language (e.g. C# or Java) will have similar issues (e.g. witness the rise of ArrayPool<T> in C#.) Garbage collection has gotten better over time, but once you hit performance issues with GC you will have to take measures like Array pooling etc. In many real world cases though, a hundred millisecond hiccup every so often isn't that big a deal - if you' have a use case where it does matter, then either you use array pooling for everything, disable the GC and just keep terminating the process and restarting it every so often, or just write the thing in a non gc language.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
C# has struct types that can be allocated on the stack. In my own projects I just have globals for scratchpad storage, and then write everything in this style:

JavaScript code:
const scratchVec3 = vec3.create();
function calcParticleBasis(dstMtx, particle) {
    // Right vector.
    vec3.cross(scratchVec3a, particle.up, particle.front);
    vec3.normalize(scratchVec3a);

    mat4FromBasisVectors(dstMtx, particle.up, particle.front, scratchVec3);
}
It sucks, but it is what it is.

What even more sucks is new-style java script: using for...of will create a new object every iteration. Katelyn Gadd (someone who is consistently ahead of me in finding about this and yelling about things) yelled about this in 2015, and nobody cared. "Engines will be optimized", they said. They still haven't, four years later. fuckin lmao i guess

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Suspicious Dish posted:

What even more sucks is new-style java script: using for...of will create a new object every iteration. Katelyn Gadd (someone who is consistently ahead of me in finding about this and yelling about things) yelled about this in 2015, and nobody cared. "Engines will be optimized", they said. They still haven't, four years later. fuckin lmao i guess
Is that true even if your for..of doesn't even involve a new object, like
code:
let p;
for (p of arrayOfObjects) {
  ...
}
Though if you do that then of course it's super error prone because if you do someAsyncThing(p) in that loop then they'll likely all end up referring to the wrong element, eg.
code:
for (p of [{a:1}, {a:2}, {a:3}]) {setTimeout(() => console.log(p), 0);}

Output
{a:3}
{a:3}
{a:3}
(Though you can also provoke that behavior by not putting a let in your for (i=0; i<3; i++) style loops.)

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
the iterator protocol says that for...of is syntactic sugar for this expanded code:

JavaScript code:
while (true) {
    const { value, done } = iter.next();
    if (done)
        break;
    // Loop body here with "value"
}
Those intermediate objects with "done" and "value" need to be created every single iteration. You can see that if you call the iterator stuff manually:

JavaScript code:
> arr = [1, 2, 3]
(3) [1, 2, 3]
> iter = arr[Symbol.iterator]()
Array Iterator {}
> iter.next()
{value: 1, done: false}
> iter.next()
{value: 2, done: false}
> iter.next()
{value: 3, done: false}
> iter.next()
{value: undefined, done: true}
Pure garbage.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Suspicious Dish posted:

the iterator protocol says that for...of is syntactic sugar for this expanded code:
Oh god. Can we just burn all the languages that aren't C++?

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

roomforthetuna posted:

Oh god. Can we just burn all the languages that aren't C++?

I could talk non-stop for weeks about poo poo in C++ I hate, but hey, at least it doesn't have GC pauses.

Munkeymon
Aug 14, 2003

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



Suspicious Dish posted:

C# has struct types that can be allocated on the stack. In my own projects I just have globals for scratchpad storage, and then write everything in this style:

JavaScript code:
const scratchVec3 = vec3.create();
function calcParticleBasis(dstMtx, particle) {
    // Right vector.
    vec3.cross(scratchVec3a, particle.up, particle.front);
    vec3.normalize(scratchVec3a);

    mat4FromBasisVectors(dstMtx, particle.up, particle.front, scratchVec3);
}
It sucks, but it is what it is.

What even more sucks is new-style java script: using for...of will create a new object every iteration. Katelyn Gadd (someone who is consistently ahead of me in finding about this and yelling about things) yelled about this in 2015, and nobody cared. "Engines will be optimized", they said. They still haven't, four years later. fuckin lmao i guess

JavaScript [Questions Megathread]: It sucks, but it it what it is.

Sab669
Sep 24, 2009

:sigh: Weird issue where I'm trying to just pass my form off to the server but 1 specific property isn't having its value set correctly for some unknown reason.

cshtml defines a checkbox bound to a Model property like so:
code:
@(Html.Kendo().CheckBoxFor(e => e.MiniFIM).Label("Perform PAC-CFI Assessment").HtmlAttributes(new { id = "chkMiniFIMIntFup", @tabIndex = "1", onclick = "PAIValidateMiniFim()" }))
Javascript uses some jquery ObjectToJSON function to convert the entire form to a JSON object that we then pass to the server with ajax:
JavaScript code:
var PAIIntFupAss = ObjectToJSON($('#frmPAIIntFupAss').serializeArray());
PAIIntFupAss.MiniFIM is "false", even though $("#chkMiniFIMIntFup").val() returns "true". I set a breakpoint on this line and see the checkbox has the epxected value, but yea the object gets a different value and I don't know why.

If I try to manually add PAIIntFupAss.MiniFIM = $("#chkMiniFIMIntFup").val(); on the following line, the C# throws an error along the lines of "MiniFIM is already defined" or something to that effect? Sounds like this assignment is adding a second 'key' named "MiniFIM" to the dictionary somehow :shrug:

If I add a watch for $('#frmPAIIntFupAss') I can expand it, find my checkbox, and it has the value true / name is MiniFIM so ObjectToJSON should be working correctly? I have no idea what's going wrong.

And that is the only control bound to that property :downs:

Sab669 fucked around with this message at 21:12 on Jul 25, 2019

Munkeymon
Aug 14, 2003

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



What's the HTML output look like?

Sab669
Sep 24, 2009

Page Inspector gives me this:

Checked

<input checked="checked" class="k-checkbox" id="chkMiniFIMIntFup" name="MiniFIM" onclick="PAIValidateMiniFim()" tabindex="1" type="checkbox" value="true">
<label class="k-checkbox-label" for="chkMiniFIMIntFup">Perform PAC-CFI Assessment</label>
<input name="MiniFIM" type="hidden" value="false">


Unchecked:

<input checked="checked" class="k-checkbox" id="chkMiniFIMIntFup" name="MiniFIM" onclick="PAIValidateMiniFim()" tabindex="1" type="checkbox" value="true">
<label class="k-checkbox-label" for="chkMiniFIMIntFup">Perform PAC-CFI Assessment</label>
<input name="MiniFIM" type="hidden" value="false">


So actually, that's really curious. I assume it's pulling the value from this hidden Input rather than the checkbox? But this checkbox is defined identically to another checkbox on a different form that basically does the same thing :sigh: Literally the only difference is the ID and the onclick (which just shows a popup, then clears some textboxes if they click YES, does nothing / unchecks the checkbox if they click NO)

Sab669 fucked around with this message at 14:16 on Jul 26, 2019

Munkeymon
Aug 14, 2003

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



The JS serializer is probably overwriting the first value with the false value it finds second. IIRC, ASP MVC goes with the first one it finds*, but it's strange that your input is checked when you're saying it's unchecked - are you looking at the live DOM or viewing the source?

* When checked, both values are submitted and the deserializer would just go with the first one it sees for the value. When unchecked, only false is submitted. I'm not sure you need the hidden input if you use a form model since the property would default to false, but there are ~5 ways to turn form data into POCOs/values of various sorts (without even considering JSON!) so YMMV

Munkeymon fucked around with this message at 14:29 on Jul 26, 2019

Sab669
Sep 24, 2009

Munkeymon posted:

are you looking at the live DOM or viewing the source?

Live, I guess? Run the software -> Hit F12 -> Inspector tab (Firefox)


The hidden input must be some Telerik/Kendo nonsense. I definitely didn't manually define it myself.

code:
<td>
	@(Html.Kendo().CheckBoxFor(e => e.MiniFIM).Label("Perform PAC-CFI Assessment").HtmlAttributes(new { id = "chkMiniFIMIntFup", @tabIndex = "1", onclick = "PAIValidateMiniFim()" }))
</td>
Is the "full" declaration for that checkbox within its parent table data element. Those 2 inputs and the label from my previous post are all found within the <td> I defined when I pull up the Inspector.

Ape Fist
Feb 23, 2007

Nowadays, you can do anything that you want; anal, oral, fisting, but you need to be wearing gloves, condoms, protection.
I love working in ASP.NET MVC as the Senior Front End at the company and being repeatedly told by the .NET devs that they're full stack because they know jQuery because it comes bundled with ASP.NET MVC 4 and therefore we really don't need anything else because they've got it covered :smugndar:

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I thought jQuery was only used today by people who don't know how to use anything else

Munkeymon
Aug 14, 2003

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



Sab669 posted:

Live, I guess? Run the software -> Hit F12 -> Inspector tab (Firefox)


The hidden input must be some Telerik/Kendo nonsense. I definitely didn't manually define it myself.

code:
<td>
	@(Html.Kendo().CheckBoxFor(e => e.MiniFIM).Label("Perform PAC-CFI Assessment").HtmlAttributes(new { id = "chkMiniFIMIntFup", @tabIndex = "1", onclick = "PAIValidateMiniFim()" }))
</td>
Is the "full" declaration for that checkbox within its parent table data element. Those 2 inputs and the label from my previous post are all found within the <td> I defined when I pull up the Inspector.

Yeah, I'm saying that's a fairly normal way to do it so that there's always a value sent to the server on form submit. Left unstated was the fact that a checkbox input that's not checked is just omitted from the data sent by a browser POSTing a form. Frameworks that want to see some value no matter what do that checkbox+hidden trick and I think what's going on is that the serialization your JS is doing has the opposite behavior of that of MVC, so HTML output assuming MVC will consume the form data doesn't work right.

You might have to crack open the JS serialization code and change the behavior such that it refuses to overwrite values when it sees the same name a second time.

Of course I might be wrong about all of this if you're not seeing the DOM change when you uncheck that box. Kendo might be doing something silly in handlers that's supposed to be tracking all of these values.

stoops
Jun 11, 2001
I have a UL with a couple of LI's with classes "fa-chevron-up/down". When the class is clicked, the function gets called. That is working when the page is loaded.

I also dynamically create new LI's, but when I do this , the functions stop working.

Currently I have my script as such. How do i make it so the new LI's will also call the .fa-chevron functions?

the dynamically created Li's are exactly in the same structure as before in the UL

<code>
$(function () {
var selected=0;
var itemlist = $('#finalList');
var len=$(itemlist).children().length;

$(".fa-chevron-up").click(function(e){
var selected= $(this).parents('li').index()
e.preventDefault();
if(selected>0){
jQuery($(itemlist).children().eq(selected-1)).before(jQuery($(itemlist).children().eq(selected)));
selected=selected-1;
}
});

$(".fa-chevron-down").click(function(e){
var selected= $(this).parents('li').index()
e.preventDefault();
if(selected < len){
jQuery($(itemlist).children().eq(selected+1)).after(jQuery($(itemlist).children().eq(selected)));
selected=selected+1;
}
});
});
</code>

Adbot
ADBOT LOVES YOU

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice
Because your code only assigns event handlers to elements that exist on load. New elements don't get them. Looks like you are using jQuery, so you will want to .on to make it so the new ones also get the event.

It's been a very, very long time since I did jQuery, but I think something like this:

JavaScript code:
$(body).on('click', 'myClassNameUp', function() { ... })

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