Git Product home page Git Product logo

Comments (6)

fdaciuk avatar fdaciuk commented on July 30, 2024

Hey @kazzkiq! I'm glad you're enjoying it =)
But, I don't know.. Can you show me a real example for this problem?

The point is: every call to ajax() function creates a new XMLHttpRequest instance.
If you want to force a base url, it will be necessary to add a static method on ajax object, like this:

ajax.baseUrl('http://example.com')

And then, every call to ajax() would be using this base url. But that seems not the right way to do it xD

I'm thinking about it. Give me a bit of time xD

from ajax.

kazzkiq avatar kazzkiq commented on July 30, 2024

@fdaciuk Yeah thats exactly what I thought. As ajax() is a singleton, by setting a baseUrl you would be changing all the other AJAX calls too.

Thats the biggest downside of this approach. If a developer overrides it from another place, all other devs/AJAX calls are affected too (although I think this can easily be solved with a proper app architecture).

However, as I said, most client-side applications would always consume from the same API, so despite the issue above, it also makes sense to set a baseUrl as default and only change that on specific cases.

Real example:

Imagine you're building a Twitter client. You're going to enable search, feed, hashtag filters and such. All those actions are under an API that use the same base URL: https://api.twitter.com/1.1/. The same goes for Facebook, and for other big services, and for mainstream projects too. You generally always consume from the same place. And what if the API changes? Would you need to replace all your ajax() URLs? Why not just replace the baseUrl() value and watch it work flawlessly?

If this is not true for some application, the user can simply not register any baseUrl. However, in most common cases I think it would be useful to configure this baseUrl on startup and then just use ajax() omitting the base of the URL.


Of course one could simply create an abstract class which sets an URL and then pass it like:

ajax().get(API.baseUrl + '/etc/abc')

However I think a solution built in the library would aggregate to its value and usability.

from ajax.

fdaciuk avatar fdaciuk commented on July 30, 2024

Hey @kazzkiq! Sorry for the delay xD

In actual version, this is impossible without cause bugs, because I am using return object with then, catch and always methods as a reference.

But the good news is: I am working in a v2 for this library, in a more functional approach, and I have ever implemented this feature xD

Look here: https://github.com/fdaciuk/ajax/tree/v2.0.0-beta

All tests are passing! If you could test, I really thank you =)

If you are using NPM, just install with npm i --save @fdaciuk/ajax@beta

from ajax.

kazzkiq avatar kazzkiq commented on July 30, 2024

Hey @fdaciuk , I did a rapid test here and it worked like a charm! Very good! :)

from ajax.

fdaciuk avatar fdaciuk commented on July 30, 2024

Thanks @kazzkiq 💃 💃 ✨

from ajax.

fdaciuk avatar fdaciuk commented on July 30, 2024

It was added on v2.0 =)
Thanks for your contribution :D

from ajax.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.