|
Use redux, do your request stuff in redux action creators via redux-pack, use reselect to bind your child components to the specific bits of the redux store being used.
|
# ? Aug 31, 2017 19:34 |
|
|
# ? May 15, 2024 23:56 |
|
Welp just learned about fetch. Holy crap
|
# ? Sep 1, 2017 04:57 |
|
Yeah fetch is awesome. It has one glaring flaw, however: requests cannot be cancelled! This isn't necessary in a lot of situations, but does mean falling back to an XHR wrapper with cancellation if you need it.
|
# ? Sep 1, 2017 14:48 |
|
Nicer than using JQuery's AJAX, but wish there was a way to circumvent the async syntax associated with it and the related json() func.
|
# ? Sep 1, 2017 15:54 |
|
Dominoes posted:Nicer than using JQuery's AJAX, but wish there was a way to circumvent the async syntax associated with it and the related json() func. You can use it in a purely Promise based way, and you can always just use text().then( (text) => { return JSON.parse(text); }) to handle the parsing yourself.
|
# ? Sep 1, 2017 16:10 |
|
The tutorial I was watching said that the great thing about fetch was that it returned a promise. But that's the same for JQuery and XHR requests correct? teen phone cutie fucked around with this message at 18:11 on Sep 5, 2017 |
# ? Sep 1, 2017 18:23 |
|
Grump posted:The tutorial I was watching said that the great thing about fetch was that it returned a promise. I guess? Promises are just a better way to manage callbacks.
|
# ? Sep 1, 2017 18:47 |
|
I meant 'the same' instead of 'try' LOL that sentence didn't make any sense
|
# ? Sep 1, 2017 18:54 |
|
What's a good way to test if a string contains any of a list of strings? if (foobar.indexOf('CNN') >= 0) { doSomething } else if (foobar.indexOf('MSNBC') >= 0) { doSomething } else if(foobar.indexOf('ABCNEWS') >= 0 { doSomething } else if(foobar.indexOf('FOXNEWS') >= 0 { doSomeOtherThing } Does something like ifStringContains(foobar, 'CNN', 'MSNBC', 'FOXNEWS') exist or should I write my own?
|
# ? Sep 1, 2017 20:13 |
|
Simple, lovely monkey-patch version:code:
|
# ? Sep 1, 2017 20:23 |
|
Bob Morales posted:What's a good way to test if a string contains any of a list of strings? String.includes is part of the standard. Just not supported by previous browsers. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes We use this and the polyfill for IE11 in production. We don't support previous to that, so I can't speak to what works. Edit: I hate to say RTFM, but I did this summer on a project that had me doing some things with JavaScript that needed to be in VanillaJS and I basically refreshed myself on how rich JS actually is in its API. MDN has really be an invaluable resource. geeves fucked around with this message at 22:12 on Sep 1, 2017 |
# ? Sep 1, 2017 22:07 |
|
i think doing a regex test would be the cleanest /(CNN|MSNBC)/i.test(thing) and if you just want to make a list of strings, you could pass it thru a regex escape func and merge them into a neato regex.
|
# ? Sep 1, 2017 22:44 |
|
geeves posted:String.includes is part of the standard. Just not supported by previous browsers. I don't think he's looking for an alternative to indexOf
|
# ? Sep 2, 2017 00:37 |
|
Bob Morales posted:What's a good way to test if a string contains any of a list of strings? JavaScript code:
|
# ? Sep 2, 2017 15:55 |
|
Grump posted:The tutorial I was watching said that the great thing about fetch was that it returned a promise. XHR does not return a promise, it uses events. jQuery returns a jQuery.deferred object (or did last time I looked which was forever ago).
|
# ? Sep 4, 2017 03:48 |
|
necrotic posted:XHR does not return a promise, it uses events. jQuery returns a jQuery.deferred object (or did last time I looked which was forever ago). Deferred is a "thenable", similar to Promise but with different APIs and runtime semantics (worse in every way). It's easy to convert to a real promise with Promise.resolve() or by just using async/await.
|
# ? Sep 4, 2017 04:23 |
|
Hey dudes, looking for wisdom on moving from Jquery AJAX requests to fetch. Here's what I've got: Get works OK, but my Django server is getting empty post data:JavaScript code:
JavaScript code:
Dominoes fucked around with this message at 19:15 on Sep 4, 2017 |
# ? Sep 4, 2017 18:15 |
|
JavaScript code:
JSON.stringify should be right, can you verify/post the actual request made by the browser?
|
# ? Sep 4, 2017 18:52 |
|
How do I do that? I looked it up/clicked F12/Network in chrome, then clicked the request; it showed me a big server-side log.
|
# ? Sep 4, 2017 19:01 |
|
Dominoes posted:How do I do that? I looked it up/clicked F12/Network in chrome, then clicked the request; it showed me a big server-side log. You're on either the preview or response tabs and that's an error log from your server so it should probably have something in there about why it's blowing up. Click the Headers tab and then look under Request Payload at the bottom right, it should have your post object ({type: 'export'}).
|
# ? Sep 4, 2017 19:27 |
|
Dominoes posted:Hey dudes, looking for wisdom on moving from Jquery AJAX requests to fetch. Here's what I've got: Get works OK, but my Django server is getting empty post data: I don't think there's a need for a simpler package. One of the problems with npm is that 90% of the packages are like 10 lines of code. Fetch is basically exactly as complicated as it needs to be to cover the common use cases. In your application, you select out the parts of the fetch API you need and wrap them in a 5 line function.
|
# ? Sep 4, 2017 20:30 |
|
Dogcow posted:You're on either the preview or response tabs and that's an error log from your server so it should probably have something in there about why it's blowing up. code:
Thermopyle posted:I don't think there's a need for a simpler package. One of the problems with npm is that 90% of the packages are like 10 lines of code. Compare the fetch api to Requests.
|
# ? Sep 5, 2017 01:38 |
|
Dominoes posted:Sounds like an indication of a sparse standard library / verbose APIs. This is exactly right and the current NPM situation is a terrible solution to that problem. Dominoes posted:Compare the fetch api to Requests. The fetch api is pretty elegant compared to what requests is hiding. It doesn't hide as much details as Requests, but it's also trivial to wrap the fetch API. Requests does a ton of work to make it as easy to use as it is. Also, there's already (as per what you'd expect with NPM and the JS ecosystem), well over a 100 fetch wrappers out there...and a brief perusal of a few of them show them to be extremely minimal thingamabobs. (see above xkcd for the problem with this) I mean, I don't think there's anything exactly wrong with writing the 200th fetch wrapper, but I question its usefulness. Dominoes posted:I already consider Lodash effectively part of JS. https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore
|
# ? Sep 5, 2017 02:03 |
I got bitten by this today because I'm so used to typescript that I used Object.values in a normal javascript project months ago and today somebody discovered that it didn't work in older iphones.
|
|
# ? Sep 5, 2017 02:10 |
|
http://vanilla-js.com/
|
# ? Sep 5, 2017 02:42 |
|
Thermopyle posted:The fetch api is pretty elegant compared to what requests is hiding. quote:Also, there's already (as per what you'd expect with NPM and the JS ecosystem), well over a 100 fetch wrappers out there...and a brief perusal of a few of them show them to be extremely minimal thingamabobs. (see above xkcd for the problem with this) Nice! Going to ref that each time I try to use a Lodash func. Dominoes fucked around with this message at 02:54 on Sep 5, 2017 |
# ? Sep 5, 2017 02:49 |
|
Just make your fetch wrapper an npm module with fetch as a dependency. Then you just need to install and include it when you want to use it. dont do this
|
# ? Sep 5, 2017 03:25 |
|
Dominoes posted:
If you just want to auto-serialize and use the same headers, just write a function that does that. Don't try to make a package out of it. You can take DRY too far.
|
# ? Sep 5, 2017 03:35 |
I really would not like to give up lodash's matcher shorthands. They're super nice once you're used to them, especially the matchesProperty ones. code:
|
|
# ? Sep 5, 2017 13:23 |
|
Osmosisch posted:I really would not like to give up lodash's matcher shorthands. They're super nice once you're used to them, especially the matchesProperty ones. But.. if you use ES6 syntax the native array filter method is actually shorter in most of your examples JavaScript code:
JavaScript code:
|
# ? Sep 5, 2017 14:38 |
|
-delete
|
# ? Sep 5, 2017 15:09 |
|
Dogcow posted:You're on either the preview or response tabs and that's an error log from your server so it should probably have something in there about why it's blowing up. Working Jquery post on the left; empty-data Fetch post on the right. I've tweaked the headers to match the working Jquery example; looks like the data format's different. I'm guessing the form-urlencoded Content-Type is the format shown in the lower-left; Should I try to get my Object into that format? Chnaging the Content-Type to 'application/json' doesn't work. edit: Looks like this is a server-side issue, probably due to the apparently two ways post data can be sent? The Jquery/form-urlencoded data is requested in Django with Python code:
Python code:
JavaScript code:
Dominoes fucked around with this message at 16:29 on Sep 5, 2017 |
# ? Sep 5, 2017 15:47 |
|
The issue looks to be that your backend wants Content-Type: "application/x-www-form-urlencoded", which is what that type=add&personId=126 is. What you are sending with fetch is Content-Type: "application/json".
|
# ? Sep 5, 2017 16:29 |
|
Yea - I'm still trying to understand this, but have now replaced all my Jquery Ajax calls with calls to my post function above. Had to change server code as well per my above post (STS!) Added bonus of cleaner deserialization server-side: Had to do weird indexing I didn't understood before that's not required with this style.
|
# ? Sep 5, 2017 20:06 |
|
The jQuery one is using the same content type as a regular form post, which encodes the body like a query string in a URL. That's all there is to it: your fetch approach is using a different content type.
|
# ? Sep 5, 2017 20:17 |
|
necrotic posted:The jQuery one is using the same content type as a regular form post, which encodes the body like a query string in a URL. That's all there is to it: your fetch approach is using a different content type.
|
# ? Sep 5, 2017 21:20 |
|
Can anyone help me with Javascript build tools? I've developed a plugin for another library (leaflet.js) that I'd like to release on github, but the source code ended up being a lot longer than I originally predicted (~1200 lines now) so I'd like to split it up into multiple files. I don't need 'require' or anything of the sort, and my code is all plain, vanilla Javascript. I basically just want some standard build tool that will take my files and concat them together, and then minify the resulting file. What's the easiest way to do this with bower, or jake, or webpack, or rollup, or whatever the heck is the latest fashion these days?
|
# ? Sep 6, 2017 02:06 |
|
German Joey posted:Can anyone help me with Javascript build tools? I've developed a plugin for another library (leaflet.js) that I'd like to release on github, but the source code ended up being a lot longer than I originally predicted (~1200 lines now) so I'd like to split it up into multiple files. I don't need 'require' or anything of the sort, and my code is all plain, vanilla Javascript. I basically just want some standard build tool that will take my files and concat them together, and then minify the resulting file. What's the easiest way to do this with bower, or jake, or webpack, or rollup, or whatever the heck is the latest fashion these days? 1200 lines isn't too bad. If you're going to split it up, make sure it makes sense in context and that they could potentially be used independently with other projects. Sometimes tools are overblown. You could try closure compiler or Uglify with a simple process. Create build.sh (chmod 755) code:
|
# ? Sep 6, 2017 02:42 |
|
If your'e OK with using typescript, this is a working webpack.config.js file:JavaScript code:
JavaScript code:
|
# ? Sep 6, 2017 02:43 |
|
|
# ? May 15, 2024 23:56 |
Dogcow posted:But.. if you use ES6 syntax the native array filter method is actually shorter in most of your examples You're right, but I was not taking ES6 into account. We're not doing transpile stuff at the moment. The nice thing about lodash is that it lets you do all this stuff without worrying about which browser/platform you're targeting too much. Performance-wise, lodash beats native in quite a few cases last I checked, though that's besides the point most of the time.
|
|
# ? Sep 6, 2017 09:39 |